Current section

Files

Jump to
corex priv static checkbox.mjs
Raw

priv/static/checkbox.mjs

import {
isFocusVisible,
trackFocusVisible
} from "./chunk-KF3PY6Q6.mjs";
import {
Component,
VanillaMachine,
canPushEvent,
createAnatomy,
createGuards,
createMachine,
dataAttr,
dispatchInputCheckedEvent,
getBoolean,
getDir,
getEventTarget,
getString,
normalizeProps,
setElementChecked,
trackFormControl,
trackPress,
visuallyHiddenStyle
} from "./chunk-ZOODJA3P.mjs";
// ../node_modules/.pnpm/@zag-js+checkbox@1.36.0/node_modules/@zag-js/checkbox/dist/checkbox.anatomy.mjs
var anatomy = createAnatomy("checkbox").parts("root", "label", "control", "indicator");
var parts = anatomy.build();
// ../node_modules/.pnpm/@zag-js+checkbox@1.36.0/node_modules/@zag-js/checkbox/dist/checkbox.dom.mjs
var getRootId = (ctx) => ctx.ids?.root ?? `checkbox:${ctx.id}`;
var getLabelId = (ctx) => ctx.ids?.label ?? `checkbox:${ctx.id}:label`;
var getControlId = (ctx) => ctx.ids?.control ?? `checkbox:${ctx.id}:control`;
var getHiddenInputId = (ctx) => ctx.ids?.hiddenInput ?? `checkbox:${ctx.id}:input`;
var getRootEl = (ctx) => ctx.getById(getRootId(ctx));
var getHiddenInputEl = (ctx) => ctx.getById(getHiddenInputId(ctx));
// ../node_modules/.pnpm/@zag-js+checkbox@1.36.0/node_modules/@zag-js/checkbox/dist/checkbox.connect.mjs
function connect(service, normalize) {
const { send, context, prop, computed, scope } = service;
const disabled = !!prop("disabled");
const readOnly = !!prop("readOnly");
const required = !!prop("required");
const invalid = !!prop("invalid");
const focused = !disabled && context.get("focused");
const focusVisible = !disabled && context.get("focusVisible");
const checked = computed("checked");
const indeterminate = computed("indeterminate");
const checkedState = context.get("checked");
const dataAttrs = {
"data-active": dataAttr(context.get("active")),
"data-focus": dataAttr(focused),
"data-focus-visible": dataAttr(focusVisible),
"data-readonly": dataAttr(readOnly),
"data-hover": dataAttr(context.get("hovered")),
"data-disabled": dataAttr(disabled),
"data-state": indeterminate ? "indeterminate" : checked ? "checked" : "unchecked",
"data-invalid": dataAttr(invalid),
"data-required": dataAttr(required)
};
return {
checked,
disabled,
indeterminate,
focused,
checkedState,
setChecked(checked2) {
send({ type: "CHECKED.SET", checked: checked2, isTrusted: false });
},
toggleChecked() {
send({ type: "CHECKED.TOGGLE", checked, isTrusted: false });
},
getRootProps() {
return normalize.label({
...parts.root.attrs,
...dataAttrs,
dir: prop("dir"),
id: getRootId(scope),
htmlFor: getHiddenInputId(scope),
onPointerMove() {
if (disabled) return;
send({ type: "CONTEXT.SET", context: { hovered: true } });
},
onPointerLeave() {
if (disabled) return;
send({ type: "CONTEXT.SET", context: { hovered: false } });
},
onClick(event) {
const target = getEventTarget(event);
if (target === getHiddenInputEl(scope)) {
event.stopPropagation();
}
}
});
},
getLabelProps() {
return normalize.element({
...parts.label.attrs,
...dataAttrs,
dir: prop("dir"),
id: getLabelId(scope)
});
},
getControlProps() {
return normalize.element({
...parts.control.attrs,
...dataAttrs,
dir: prop("dir"),
id: getControlId(scope),
"aria-hidden": true
});
},
getIndicatorProps() {
return normalize.element({
...parts.indicator.attrs,
...dataAttrs,
dir: prop("dir"),
hidden: !indeterminate && !checked
});
},
getHiddenInputProps() {
return normalize.input({
id: getHiddenInputId(scope),
type: "checkbox",
required: prop("required"),
defaultChecked: checked,
disabled,
"aria-labelledby": getLabelId(scope),
"aria-invalid": invalid,
name: prop("name"),
form: prop("form"),
value: prop("value"),
style: visuallyHiddenStyle,
onFocus() {
const focusVisible2 = isFocusVisible();
send({ type: "CONTEXT.SET", context: { focused: true, focusVisible: focusVisible2 } });
},
onBlur() {
send({ type: "CONTEXT.SET", context: { focused: false, focusVisible: false } });
},
onClick(event) {
if (readOnly) {
event.preventDefault();
return;
}
const checked2 = event.currentTarget.checked;
send({ type: "CHECKED.SET", checked: checked2, isTrusted: true });
}
});
}
};
}
// ../node_modules/.pnpm/@zag-js+checkbox@1.36.0/node_modules/@zag-js/checkbox/dist/checkbox.machine.mjs
var { not } = createGuards();
var machine = createMachine({
props({ props }) {
return {
value: "on",
...props,
defaultChecked: props.defaultChecked ?? false
};
},
initialState() {
return "ready";
},
context({ prop, bindable }) {
return {
checked: bindable(() => ({
defaultValue: prop("defaultChecked"),
value: prop("checked"),
onChange(checked) {
prop("onCheckedChange")?.({ checked });
}
})),
fieldsetDisabled: bindable(() => ({ defaultValue: false })),
focusVisible: bindable(() => ({ defaultValue: false })),
active: bindable(() => ({ defaultValue: false })),
focused: bindable(() => ({ defaultValue: false })),
hovered: bindable(() => ({ defaultValue: false }))
};
},
watch({ track, context, prop, action }) {
track([() => prop("disabled")], () => {
action(["removeFocusIfNeeded"]);
});
track([() => context.get("checked")], () => {
action(["syncInputElement"]);
});
},
effects: ["trackFormControlState", "trackPressEvent", "trackFocusVisible"],
on: {
"CHECKED.TOGGLE": [
{
guard: not("isTrusted"),
actions: ["toggleChecked", "dispatchChangeEvent"]
},
{
actions: ["toggleChecked"]
}
],
"CHECKED.SET": [
{
guard: not("isTrusted"),
actions: ["setChecked", "dispatchChangeEvent"]
},
{
actions: ["setChecked"]
}
],
"CONTEXT.SET": {
actions: ["setContext"]
}
},
computed: {
indeterminate: ({ context }) => isIndeterminate(context.get("checked")),
checked: ({ context }) => isChecked(context.get("checked")),
disabled: ({ context, prop }) => !!prop("disabled") || context.get("fieldsetDisabled")
},
states: {
ready: {}
},
implementations: {
guards: {
isTrusted: ({ event }) => !!event.isTrusted
},
effects: {
trackPressEvent({ context, computed, scope }) {
if (computed("disabled")) return;
return trackPress({
pointerNode: getRootEl(scope),
keyboardNode: getHiddenInputEl(scope),
isValidKey: (event) => event.key === " ",
onPress: () => context.set("active", false),
onPressStart: () => context.set("active", true),
onPressEnd: () => context.set("active", false)
});
},
trackFocusVisible({ computed, scope }) {
if (computed("disabled")) return;
return trackFocusVisible({ root: scope.getRootNode?.() });
},
trackFormControlState({ context, scope }) {
return trackFormControl(getHiddenInputEl(scope), {
onFieldsetDisabledChange(disabled) {
context.set("fieldsetDisabled", disabled);
},
onFormReset() {
context.set("checked", context.initial("checked"));
}
});
}
},
actions: {
setContext({ context, event }) {
for (const key in event.context) {
context.set(key, event.context[key]);
}
},
syncInputElement({ context, computed, scope }) {
const inputEl = getHiddenInputEl(scope);
if (!inputEl) return;
setElementChecked(inputEl, computed("checked"));
inputEl.indeterminate = isIndeterminate(context.get("checked"));
},
removeFocusIfNeeded({ context, prop }) {
if (prop("disabled") && context.get("focused")) {
context.set("focused", false);
context.set("focusVisible", false);
}
},
setChecked({ context, event }) {
context.set("checked", event.checked);
},
toggleChecked({ context, computed }) {
const checked = isIndeterminate(computed("checked")) ? true : !computed("checked");
context.set("checked", checked);
},
dispatchChangeEvent({ computed, scope }) {
queueMicrotask(() => {
const inputEl = getHiddenInputEl(scope);
dispatchInputCheckedEvent(inputEl, { checked: computed("checked") });
});
}
}
}
});
function isIndeterminate(checked) {
return checked === "indeterminate";
}
function isChecked(checked) {
return isIndeterminate(checked) ? false : !!checked;
}
// components/checkbox.ts
var Checkbox = class extends Component {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
initMachine(props) {
return new VanillaMachine(machine, props);
}
initApi() {
return connect(this.machine.service, normalizeProps);
}
render() {
const rootEl = this.el.querySelector('[data-scope="checkbox"][data-part="root"]');
if (!rootEl) return;
this.spreadProps(rootEl, this.api.getRootProps());
const inputEl = rootEl.querySelector(
':scope > [data-scope="checkbox"][data-part="hidden-input"]'
);
if (inputEl) {
this.spreadProps(inputEl, this.api.getHiddenInputProps());
}
const labelEl = rootEl.querySelector(
':scope > [data-scope="checkbox"][data-part="label"]'
);
if (labelEl) {
this.spreadProps(labelEl, this.api.getLabelProps());
}
const controlEl = rootEl.querySelector(
':scope > [data-scope="checkbox"][data-part="control"]'
);
if (controlEl) {
this.spreadProps(controlEl, this.api.getControlProps());
const indicatorEl = controlEl.querySelector(
':scope > [data-scope="checkbox"][data-part="indicator"]'
);
if (indicatorEl) {
this.spreadProps(indicatorEl, this.api.getIndicatorProps());
}
}
}
};
// hooks/checkbox.ts
var CheckboxHook = {
mounted() {
const el = this.el;
const pushEvent = this.pushEvent.bind(this);
const zagCheckbox = new Checkbox(el, {
id: el.id,
...getBoolean(el, "controlled") ? { checked: getBoolean(el, "checked") } : { defaultChecked: getBoolean(el, "defaultChecked") },
disabled: getBoolean(el, "disabled"),
name: getString(el, "name"),
form: getString(el, "form"),
value: getString(el, "value"),
dir: getDir(el),
invalid: getBoolean(el, "invalid"),
required: getBoolean(el, "required"),
readOnly: getBoolean(el, "readOnly"),
onCheckedChange: (details) => {
const eventName = getString(el, "onCheckedChange");
if (eventName && canPushEvent(this.liveSocket)) {
pushEvent(eventName, {
checked: details.checked,
id: el.id
});
}
const eventNameClient = getString(el, "onCheckedChangeClient");
if (eventNameClient) {
el.dispatchEvent(
new CustomEvent(eventNameClient, {
bubbles: true,
detail: {
id: el.id,
checked: details.checked
}
})
);
}
}
});
zagCheckbox.init();
this.checkbox = zagCheckbox;
this.onSetChecked = (event) => {
const { checked } = event.detail;
zagCheckbox.api.setChecked(checked);
};
el.addEventListener("phx:checkbox:set-checked", this.onSetChecked);
this.onToggleChecked = () => {
zagCheckbox.api.toggleChecked();
};
el.addEventListener("phx:checkbox:toggle-checked", this.onToggleChecked);
this.handlers = [];
this.handlers.push(
this.handleEvent("checkbox_set_checked", (payload) => {
const targetId = payload.id;
if (targetId && targetId !== el.id) return;
zagCheckbox.api.setChecked(payload.checked);
})
);
this.handlers.push(
this.handleEvent("checkbox_toggle_checked", (payload) => {
const targetId = payload.id;
if (targetId && targetId !== el.id) return;
zagCheckbox.api.toggleChecked();
})
);
this.handlers.push(
this.handleEvent("checkbox_checked", () => {
this.pushEvent("checkbox_checked_response", {
value: zagCheckbox.api.checked
});
})
);
this.handlers.push(
this.handleEvent("checkbox_focused", () => {
this.pushEvent("checkbox_focused_response", {
value: zagCheckbox.api.focused
});
})
);
this.handlers.push(
this.handleEvent("checkbox_disabled", () => {
this.pushEvent("checkbox_disabled_response", {
value: zagCheckbox.api.disabled
});
})
);
},
updated() {
this.checkbox?.updateProps({
id: this.el.id,
...getBoolean(this.el, "controlled") ? { checked: getBoolean(this.el, "checked") } : { defaultChecked: getBoolean(this.el, "defaultChecked") },
disabled: getBoolean(this.el, "disabled"),
name: getString(this.el, "name"),
form: getString(this.el, "form"),
value: getString(this.el, "value"),
dir: getDir(this.el),
invalid: getBoolean(this.el, "invalid"),
required: getBoolean(this.el, "required"),
readOnly: getBoolean(this.el, "readOnly"),
label: getString(this.el, "label")
});
},
destroyed() {
if (this.onSetChecked) {
this.el.removeEventListener("phx:checkbox:set-checked", this.onSetChecked);
}
if (this.onToggleChecked) {
this.el.removeEventListener("phx:checkbox:toggle-checked", this.onToggleChecked);
}
if (this.handlers) {
for (const handler of this.handlers) {
this.removeHandleEvent(handler);
}
}
this.checkbox?.destroy();
}
};
export {
CheckboxHook as Checkbox
};