Accessible form components

This commit is contained in:
Freeplay 2023-05-15 16:19:33 -04:00
parent 38a29ad2c5
commit 1cc7c3f8dd
9 changed files with 481 additions and 601 deletions

View file

@ -1,6 +1,6 @@
<template> <template>
<div class="dwzlatin" :class="{ opened }"> <details class="dwzlatin" :open="defaultOpen">
<button class="header _button" @click="toggle"> <summary class="header _button">
<span class="icon"><slot name="icon"></slot></span> <span class="icon"><slot name="icon"></slot></span>
<span class="text"><slot name="label"></slot></span> <span class="text"><slot name="label"></slot></span>
<span class="right"> <span class="right">
@ -8,36 +8,19 @@
<i v-if="opened" class="ph-caret-up ph-bold ph-lg icon"></i> <i v-if="opened" class="ph-caret-up ph-bold ph-lg icon"></i>
<i v-else class="ph-caret-down ph-bold ph-lg icon"></i> <i v-else class="ph-caret-down ph-bold ph-lg icon"></i>
</span> </span>
</button> </summary>
<KeepAlive> <div class="body">
<div v-if="openedAtLeastOnce" v-show="opened" class="body">
<MkSpacer :margin-min="14" :margin-max="22"> <MkSpacer :margin-min="14" :margin-max="22">
<slot></slot> <slot></slot>
</MkSpacer> </MkSpacer>
</div> </div>
</KeepAlive> </details>
</div>
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
const props = withDefaults( defineProps<{
defineProps<{
defaultOpen: boolean; defaultOpen: boolean;
}>(), }>();
{
defaultOpen: false,
}
);
let opened: boolean = $ref(props.defaultOpen);
let openedAtLeastOnce: boolean = $ref(props.defaultOpen);
const toggle = (): void => {
opened = !opened;
if (opened) {
openedAtLeastOnce = true;
}
};
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>
@ -100,11 +83,5 @@ const toggle = (): void => {
background: var(--panel); background: var(--panel);
border-radius: 0 0 6px 6px; border-radius: 0 0 6px 6px;
} }
&.opened {
> .header {
border-radius: 6px 6px 0 0;
}
}
} }
</style> </style>

View file

@ -1,6 +1,7 @@
<template> <template>
<div class="matxzzsk"> <div class="matxzzsk">
<div class="label" @click="focus"><slot name="label"></slot></div> <label>
<div class="label"><slot name="label"></slot></div>
<div class="input" :class="{ inline, disabled, focused }"> <div class="input" :class="{ inline, disabled, focused }">
<div ref="prefixEl" class="prefix"><slot name="prefix"></slot></div> <div ref="prefixEl" class="prefix"><slot name="prefix"></slot></div>
<input <input
@ -28,6 +29,7 @@
<div ref="suffixEl" class="suffix"><slot name="suffix"></slot></div> <div ref="suffixEl" class="suffix"><slot name="suffix"></slot></div>
</div> </div>
<div class="caption"><slot name="caption"></slot></div> <div class="caption"><slot name="caption"></slot></div>
</label>
<MkButton <MkButton
v-if="manualSave && changed" v-if="manualSave && changed"
@ -176,6 +178,7 @@ onMounted(() => {
<style lang="scss" scoped> <style lang="scss" scoped>
.matxzzsk { .matxzzsk {
> label {
> .label { > .label {
font-size: 0.85em; font-size: 0.85em;
padding: 0 0 8px 0; padding: 0 0 8px 0;
@ -281,6 +284,7 @@ onMounted(() => {
} }
} }
} }
}
> .save { > .save {
margin: 8px 0 0 0; margin: 8px 0 0 0;

View file

@ -1,18 +1,20 @@
<template> <template>
<div <label
v-adaptive-border v-adaptive-border
class="novjtctn" class="novjtctn"
:class="{ disabled, checked }" :class="{ disabled, checked }"
:aria-checked="checked"
:aria-disabled="disabled"
@click="toggle"
> >
<input type="radio" :disabled="disabled" /> <input
type="radio"
:disabled="disabled"
:checked="checked"
v-on:change="(x) => toggle(x)"
/>
<span class="button"> <span class="button">
<span></span> <span></span>
</span> </span>
<span class="label"><slot></slot></span> <span class="label"><slot></slot></span>
</div> </label>
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
@ -30,7 +32,7 @@ const emit = defineEmits<{
let checked = $computed(() => props.modelValue === props.value); let checked = $computed(() => props.modelValue === props.value);
function toggle(): void { function toggle(x) {
if (props.disabled) return; if (props.disabled) return;
emit("update:modelValue", props.value); emit("update:modelValue", props.value);
} }
@ -93,8 +95,8 @@ function toggle(): void {
> input { > input {
position: absolute; position: absolute;
width: 0; width: 18px;
height: 0; height: 18px;
opacity: 0; opacity: 0;
margin: 0; margin: 0;
} }
@ -107,6 +109,7 @@ function toggle(): void {
border: solid 2px var(--inputBorder); border: solid 2px var(--inputBorder);
border-radius: 100%; border-radius: 100%;
transition: inherit; transition: inherit;
pointer-events: none;
&:after { &:after {
content: ""; content: "";

View file

@ -31,7 +31,7 @@ export default defineComponent({
options = options[0].children; options = options[0].children;
return h( return h(
"div", "fieldset",
{ {
class: "novjtcto", class: "novjtcto",
}, },
@ -39,7 +39,7 @@ export default defineComponent({
...(label ...(label
? [ ? [
h( h(
"div", "legend",
{ {
class: "label", class: "label",
}, },
@ -86,6 +86,8 @@ export default defineComponent({
<style lang="scss"> <style lang="scss">
.novjtcto { .novjtcto {
border: 0;
padding: 0;
> .label { > .label {
font-size: 0.85em; font-size: 0.85em;
padding: 0 0 8px 0; padding: 0 0 8px 0;

View file

@ -1,46 +1,46 @@
<template> <template>
<div class="timctyfi" :class="{ disabled, easing }"> <label class="timctyfi" :class="{ disabled, easing }">
<div class="label"><slot name="label"></slot></div> <div class="label"><slot name="label"></slot></div>
<div v-adaptive-border class="body"> <div v-adaptive-border class="body">
<div ref="containerEl" class="container"> <div class="container">
<div class="track"> <input
<div ref="inputEl"
class="highlight" type="range"
:style="{ width: steppedRawValue * 100 + '%' }" :min="min"
></div> :max="max"
</div> :step="step"
<div v-if="steps && showTicks" class="ticks"> :list="id"
<div :value="modelValue"
:disabled="disabled"
v-on:change="(x) => onChange(x)"
@focus="tooltipShow"
@blur="tooltipHide"
@mouseenter="tooltipShow"
@mouseleave="tooltipHide"
@input="(x) => inputVal = x.target.value"
/>
<datalist
v-if="showTicks && steps"
:id="id"
>
<option
v-for="i in steps + 1" v-for="i in steps + 1"
class="tick" :value="i"
:style="{ left: ((i - 1) / steps) * 100 + '%' }" :label="i.toString()"
></div> ></option>
</div> </datalist>
<div
ref="thumbEl"
v-tooltip="textConverter(finalValue)"
class="thumb"
:style="{ left: thumbPosition + 'px' }"
@mousedown="onMousedown"
@touchstart="onMousedown"
></div>
</div> </div>
</div> </div>
<div class="caption"><slot name="caption"></slot></div> <div class="caption"><slot name="caption"></slot></div>
</div> </label>
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
import { import { ref, computed, defineAsyncComponent } from "vue";
computed,
defineAsyncComponent,
onMounted,
onUnmounted,
ref,
watch,
} from "vue";
import * as os from "@/os"; import * as os from "@/os";
const id = os.getUniqueId();
const props = withDefaults( const props = withDefaults(
defineProps<{ defineProps<{
modelValue: number; modelValue: number;
@ -59,61 +59,13 @@ const props = withDefaults(
} }
); );
const inputEl = ref<HTMLElement>();
let inputVal = $ref(props.modelValue);
const emit = defineEmits<{ const emit = defineEmits<{
(ev: "update:modelValue", value: number): void; (ev: "update:modelValue", value: number): void;
}>(); }>();
const containerEl = ref<HTMLElement>();
const thumbEl = ref<HTMLElement>();
const rawValue = ref((props.modelValue - props.min) / (props.max - props.min));
const steppedRawValue = computed(() => {
if (props.step) {
const step = props.step / (props.max - props.min);
return step * Math.round(rawValue.value / step);
} else {
return rawValue.value;
}
});
const finalValue = computed(() => {
if (Number.isInteger(props.step)) {
return Math.round(
steppedRawValue.value * (props.max - props.min) + props.min
);
} else {
return steppedRawValue.value * (props.max - props.min) + props.min;
}
});
const thumbWidth = computed(() => {
if (thumbEl.value == null) return 0;
return thumbEl.value!.offsetWidth;
});
const thumbPosition = ref(0);
const calcThumbPosition = () => {
if (containerEl.value == null) {
thumbPosition.value = 0;
} else {
thumbPosition.value =
(containerEl.value.offsetWidth - thumbWidth.value) *
steppedRawValue.value;
}
};
watch([steppedRawValue, containerEl], calcThumbPosition);
let ro: ResizeObserver | undefined;
onMounted(() => {
ro = new ResizeObserver((entries, observer) => {
calcThumbPosition();
});
ro.observe(containerEl.value);
});
onUnmounted(() => {
if (ro) ro.disconnect();
});
const steps = computed(() => { const steps = computed(() => {
if (props.step) { if (props.step) {
return (props.max - props.min) / props.step; return (props.max - props.min) / props.step;
@ -122,71 +74,30 @@ const steps = computed(() => {
} }
}); });
const onMousedown = (ev: MouseEvent | TouchEvent) => { function onChange(x) {
ev.preventDefault(); emit("update:modelValue", inputVal);
}
const tooltipShowing = ref(true); const tooltipShowing = ref(false);
function tooltipShow() {
tooltipShowing.value = true;
os.popup( os.popup(
defineAsyncComponent(() => import("@/components/MkTooltip.vue")), defineAsyncComponent(() => import("@/components/MkTooltip.vue")),
{ {
showing: tooltipShowing, showing: tooltipShowing,
text: computed(() => { text: computed(() => {
return props.textConverter(finalValue.value); return props.textConverter(inputVal);
}), }),
targetElement: thumbEl, targetElement: inputEl,
}, },
{}, {},
"closed" "closed"
); );
}
const style = document.createElement("style"); function tooltipHide() {
style.appendChild(
document.createTextNode(
"* { cursor: grabbing !important; } body * { pointer-events: none !important; }"
)
);
document.head.appendChild(style);
const onDrag = (ev: MouseEvent | TouchEvent) => {
ev.preventDefault();
const containerRect = containerEl.value!.getBoundingClientRect();
const pointerX =
ev.touches && ev.touches.length > 0
? ev.touches[0].clientX
: ev.clientX;
const pointerPositionOnContainer =
pointerX - (containerRect.left + thumbWidth.value / 2);
rawValue.value = Math.min(
1,
Math.max(
0,
pointerPositionOnContainer /
(containerEl.value!.offsetWidth - thumbWidth.value)
)
);
};
let beforeValue = finalValue.value;
const onMouseup = () => {
document.head.removeChild(style);
tooltipShowing.value = false; tooltipShowing.value = false;
window.removeEventListener("mousemove", onDrag); }
window.removeEventListener("touchmove", onDrag);
window.removeEventListener("mouseup", onMouseup);
window.removeEventListener("touchend", onMouseup);
//
if (beforeValue !== finalValue.value) {
emit("update:modelValue", finalValue.value);
}
};
window.addEventListener("mousemove", onDrag);
window.addEventListener("touchmove", onDrag);
window.addEventListener("mouseup", onMouseup, { once: true });
window.addEventListener("touchend", onMouseup, { once: true });
};
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>
@ -228,56 +139,19 @@ const onMousedown = (ev: MouseEvent | TouchEvent) => {
position: relative; position: relative;
height: $thumbHeight; height: $thumbHeight;
> .track { @mixin track {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
width: calc(100% - #{$thumbWidth});
height: 3px; height: 3px;
background: rgba(0, 0, 0, 0.1); background: rgba(0, 0, 0, 0.1);
border-radius: 999px; border-radius: 999px;
overflow: clip;
> .highlight {
position: absolute;
top: 0;
left: 0;
height: 100%;
background: var(--accent);
opacity: 0.5;
}
} }
> .ticks { @mixin fill {
$tickWidth: 3px; background-color: var(--accent);
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
width: calc(100% - #{$thumbWidth});
> .tick {
position: absolute;
bottom: 0;
width: $tickWidth;
height: 3px;
margin-left: math.div($tickWidth, 2);
background: var(--divider);
border-radius: 999px;
}
} }
> .thumb { @mixin thumb {
position: absolute;
width: $thumbWidth; width: $thumbWidth;
height: $thumbHeight; height: $thumbHeight;
cursor: grab;
background: var(--accent); background: var(--accent);
border-radius: 999px; border-radius: 999px;
@ -285,6 +159,24 @@ const onMousedown = (ev: MouseEvent | TouchEvent) => {
background: var(--accentLighten); background: var(--accentLighten);
} }
} }
> input {
width: 100%;
background: none;
&::-webkit-slider-runnable-track { @include track }
&::-moz-range-track { @include track }
&::-ms-track { @include track }
&::-moz-range-progress { @include fill }
&::-ms-fill-lower { @include fill }
&::-webkit-slider-thumb {
margin-top: -6.5px;
@include thumb
}
&::-moz-range-thumb { @include thumb }
&::-ms-thumb { @include thumb }
}
} }
} }

View file

@ -1,11 +1,13 @@
<template> <template>
<div class="vblkjoeq"> <div class="vblkjoeq">
<div class="label" @click="focus"><slot name="label"></slot></div> <label>
<div class="label"><slot name="label"></slot></div>
<div <div
ref="container" ref="container"
class="input" class="input"
:class="{ inline, disabled, focused }" :class="{ inline, disabled, focused }"
@click.prevent="onClick" @click.prevent="onClick"
tabindex="-1"
> >
<div ref="prefixEl" class="prefix"><slot name="prefix"></slot></div> <div ref="prefixEl" class="prefix"><slot name="prefix"></slot></div>
<select <select
@ -28,6 +30,7 @@
</div> </div>
</div> </div>
<div class="caption"><slot name="caption"></slot></div> <div class="caption"><slot name="caption"></slot></div>
</label>
<MkButton v-if="manualSave && changed" primary @click="updated" <MkButton v-if="manualSave && changed" primary @click="updated"
><i class="ph-floppy-disk-back ph-bold ph-lg"></i> ><i class="ph-floppy-disk-back ph-bold ph-lg"></i>
@ -191,6 +194,7 @@ const onClick = (ev: MouseEvent) => {
<style lang="scss" scoped> <style lang="scss" scoped>
.vblkjoeq { .vblkjoeq {
> label {
> .label { > .label {
font-size: 0.85em; font-size: 0.85em;
padding: 0 0 8px 0; padding: 0 0 8px 0;
@ -303,5 +307,6 @@ const onClick = (ev: MouseEvent) => {
} }
} }
} }
}
} }
</style> </style>

View file

@ -1,31 +1,26 @@
<template> <template>
<div class="ziffeomt" :class="{ disabled, checked }"> <label class="ziffeomt">
<input <input
ref="input"
type="checkbox" type="checkbox"
:checked="modelValue"
:disabled="disabled" :disabled="disabled"
@keydown.enter="toggle" v-on:change="(x) => toggle(x)"
/> />
<span <div
ref="button"
v-tooltip="checked ? i18n.ts.itsOn : i18n.ts.itsOff"
class="button" class="button"
@click.prevent="toggle"
> >
<div class="knob"></div> <div class="knob"></div>
</span> </div>
<span class="label"> <span class="label">
<!-- TODO: 無名slotの方は廃止 --> <!-- TODO: 無名slotの方は廃止 -->
<span @click="toggle"><slot name="label"></slot><slot></slot></span> <span><slot name="label"></slot><slot></slot></span>
<p class="caption"><slot name="caption"></slot></p> <p class="caption"><slot name="caption"></slot></p>
</span> </span>
</div> </label>
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
import { toRefs, Ref } from "vue"; import { Ref } from "vue";
import * as os from "@/os";
import { i18n } from "@/i18n";
const props = defineProps<{ const props = defineProps<{
modelValue: boolean | Ref<boolean>; modelValue: boolean | Ref<boolean>;
@ -36,15 +31,10 @@ const emit = defineEmits<{
(ev: "update:modelValue", v: boolean): void; (ev: "update:modelValue", v: boolean): void;
}>(); }>();
let button = $ref<HTMLElement>(); function toggle(x) {
const checked = toRefs(props).modelValue;
const toggle = () => {
if (props.disabled) return; if (props.disabled) return;
emit("update:modelValue", !checked.value); emit("update:modelValue", x.target.checked);
}
if (!checked.value) {
}
};
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>
@ -59,8 +49,8 @@ const toggle = () => {
> input { > input {
position: absolute; position: absolute;
width: 0; width: 32px;
height: 0; height: 23px;
opacity: 0; opacity: 0;
margin: 0; margin: 0;
} }
@ -78,9 +68,9 @@ const toggle = () => {
background-clip: content-box; background-clip: content-box;
border: solid 1px var(--swutchOffBg); border: solid 1px var(--swutchOffBg);
border-radius: 999px; border-radius: 999px;
cursor: pointer;
transition: inherit; transition: inherit;
user-select: none; user-select: none;
pointer-events: none;
> .knob { > .knob {
position: absolute; position: absolute;
@ -128,13 +118,12 @@ const toggle = () => {
} }
} }
&.disabled { > input:disabled ~ * {
opacity: 0.6; opacity: 0.6;
cursor: not-allowed; cursor: not-allowed;
} }
&.checked { > input:checked ~ .button {
> .button {
background-color: var(--swutchOnBg) !important; background-color: var(--swutchOnBg) !important;
border-color: var(--swutchOnBg) !important; border-color: var(--swutchOnBg) !important;
@ -143,6 +132,5 @@ const toggle = () => {
background: var(--swutchOnFg); background: var(--swutchOnFg);
} }
} }
}
} }
</style> </style>

View file

@ -1,6 +1,9 @@
<template> <template>
<div class="adhpbeos"> <div class="adhpbeos">
<div class="label" @click="focus"><slot name="label"></slot></div> <label>
<span class="label">
<slot name="label"></slot>
</span>
<div class="input" :class="{ disabled, focused, tall, pre }"> <div class="input" :class="{ disabled, focused, tall, pre }">
<textarea <textarea
ref="inputEl" ref="inputEl"
@ -21,6 +24,7 @@
></textarea> ></textarea>
</div> </div>
<div class="caption"><slot name="caption"></slot></div> <div class="caption"><slot name="caption"></slot></div>
</label>
<MkButton <MkButton
v-if="manualSave && changed" v-if="manualSave && changed"
@ -192,16 +196,15 @@ export default defineComponent({
<style lang="scss" scoped> <style lang="scss" scoped>
.adhpbeos { .adhpbeos {
> label {
> .label { > .label {
font-size: 0.85em; font-size: 0.85em;
padding: 0 0 8px 0; padding: 0 0 8px 0;
user-select: none; user-select: none;
&:empty { &:empty {
display: none; display: none;
} }
} }
> .caption { > .caption {
font-size: 0.85em; font-size: 0.85em;
padding: 8px 0 0 0; padding: 8px 0 0 0;
@ -211,7 +214,6 @@ export default defineComponent({
display: none; display: none;
} }
} }
> .input { > .input {
position: relative; position: relative;
@ -269,6 +271,8 @@ export default defineComponent({
} }
} }
} }
}
> .save { > .save {
margin: 8px 0 0 0; margin: 8px 0 0 0;

View file

@ -191,6 +191,11 @@ export function claimZIndex(
return zIndexes[priority]; return zIndexes[priority];
} }
let uniqueId = 0;
export function getUniqueId(): string {
return uniqueId++ + '';
}
export async function popup( export async function popup(
component: Component, component: Component,
props: Record<string, any>, props: Record<string, any>,