@ark-ui/vue
Version:
A collection of unstyled, accessible UI components for Vue, utilizing state machines for seamless interaction.
83 lines (78 loc) • 2.6 kB
JavaScript
;
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const domQuery = require('@zag-js/dom-query');
const vue = require('vue');
const fieldset_anatomy = require('./fieldset.anatomy.cjs');
const useFieldset = (props) => {
const state = vue.reactive({
hasErrorText: false,
hasHelperText: false
});
const rootRef = vue.ref(null);
let observer = null;
vue.onMounted(() => {
const rootNode = rootRef.value;
if (!rootNode) return;
const win = domQuery.getWindow(rootNode);
const doc = win.document;
const checkTextElements = () => {
const fieldsetProps = vue.toValue(props);
const id = fieldsetProps.id ?? vue.useId();
const errorTextId = `fieldset::${id}::error-text`;
const helperTextId = `fieldset::${id}::helper-text`;
state.hasErrorText = !!doc.getElementById(errorTextId);
state.hasHelperText = !!doc.getElementById(helperTextId);
};
checkTextElements();
observer = new win.MutationObserver(checkTextElements);
observer.observe(rootNode, { childList: true, subtree: true });
});
vue.onBeforeUnmount(() => {
observer?.disconnect();
});
return vue.computed(() => {
const fieldsetProps = vue.toValue(props);
const { disabled, invalid } = fieldsetProps;
const id = fieldsetProps.id ?? vue.useId();
const errorTextId = `fieldset::${id}::error-text`;
const helperTextId = `fieldset::${id}::helper-text`;
const labelId = `fieldset::${id}::label`;
const labelIds = [];
if (state.hasErrorText && invalid) labelIds.push(errorTextId);
if (state.hasHelperText) labelIds.push(helperTextId);
const getRootProps = () => ({
...fieldset_anatomy.parts.root.attrs,
disabled,
"data-disabled": disabled ? "true" : void 0,
"data-invalid": invalid ? "true" : void 0,
"aria-describedby": labelIds.join(" ")
});
const getLegendProps = () => ({
id: labelId,
...fieldset_anatomy.parts.legend.attrs,
"data-disabled": disabled ? "true" : void 0,
"data-invalid": invalid ? "true" : void 0
});
const getHelperTextProps = () => ({
id: helperTextId,
...fieldset_anatomy.parts.helperText.attrs
});
const getErrorTextProps = () => ({
id: errorTextId,
...fieldset_anatomy.parts.errorText.attrs,
"aria-live": "polite"
});
return {
refs: {
rootRef
},
disabled,
invalid,
getRootProps,
getLegendProps,
getHelperTextProps,
getErrorTextProps
};
});
};
exports.useFieldset = useFieldset;