@zag-js/accordion
Advanced tools
Comparing version 0.0.0-dev-20240723090825 to 0.0.0-v1-beta-20250220125322
import * as _zag_js_anatomy from '@zag-js/anatomy'; | ||
import { RequiredBy, PropTypes, DirectionProperty, CommonProperties, NormalizeProps } from '@zag-js/types'; | ||
import { DirectionProperty, CommonProperties, PropTypes, RequiredBy, NormalizeProps } from '@zag-js/types'; | ||
import * as _zag_js_core from '@zag-js/core'; | ||
import { Machine, StateMachine } from '@zag-js/core'; | ||
import { Service, EventObject } from '@zag-js/core'; | ||
@@ -20,12 +20,12 @@ declare const anatomy: _zag_js_anatomy.AnatomyInstance<"root" | "item" | "itemTrigger" | "itemContent" | "itemIndicator">; | ||
}>; | ||
interface PublicContext extends DirectionProperty, CommonProperties { | ||
interface AccordionProps extends DirectionProperty, CommonProperties { | ||
/** | ||
* The ids of the elements in the accordion. Useful for composition. | ||
*/ | ||
ids?: ElementIds; | ||
ids?: ElementIds | undefined; | ||
/** | ||
* Whether multple accordion items can be expanded at the same time. | ||
* Whether multiple accordion items can be expanded at the same time. | ||
* @default false | ||
*/ | ||
multiple?: boolean; | ||
multiple?: boolean | undefined; | ||
/** | ||
@@ -35,11 +35,15 @@ * Whether an accordion item can be closed after it has been expanded. | ||
*/ | ||
collapsible?: boolean; | ||
collapsible?: boolean | undefined; | ||
/** | ||
* The `value` of the accordion items that are currently being expanded. | ||
*/ | ||
value: string[]; | ||
value?: string[] | undefined; | ||
/** | ||
* The default value of the accordion items that are currently being expanded. | ||
*/ | ||
defaultValue?: string[] | undefined; | ||
/** | ||
* Whether the accordion items are disabled | ||
*/ | ||
disabled?: boolean; | ||
disabled?: boolean | undefined; | ||
/** | ||
@@ -57,22 +61,21 @@ * The callback fired when the state of expanded/collapsed accordion items changes. | ||
*/ | ||
orientation?: "horizontal" | "vertical"; | ||
orientation?: "horizontal" | "vertical" | undefined; | ||
} | ||
type UserDefinedContext = RequiredBy<PublicContext, "id">; | ||
type ComputedContext = Readonly<{ | ||
/** | ||
* @computed | ||
* Whether the accordion items are horizontal. | ||
*/ | ||
isHorizontal: boolean; | ||
}>; | ||
interface PrivateContext { | ||
} | ||
interface MachineContext extends PublicContext, PrivateContext, ComputedContext { | ||
} | ||
interface MachineState { | ||
value: "idle" | "focused"; | ||
} | ||
type State = StateMachine.State<MachineContext, MachineState>; | ||
type Send = StateMachine.Send<StateMachine.AnyEventObject>; | ||
type Service = Machine<MachineContext, MachineState, StateMachine.AnyEventObject>; | ||
type PropsWithDefault = "multiple" | "collapsible" | "orientation"; | ||
type AccordionSchema = { | ||
state: "idle" | "focused"; | ||
props: RequiredBy<AccordionProps, PropsWithDefault>; | ||
context: { | ||
value: string[]; | ||
focusedValue: string | null; | ||
}; | ||
computed: { | ||
isHorizontal: boolean; | ||
}; | ||
action: string; | ||
guard: string; | ||
effect: string; | ||
event: EventObject; | ||
}; | ||
type AccordionService = Service<AccordionSchema>; | ||
interface ItemProps { | ||
@@ -86,3 +89,3 @@ /** | ||
*/ | ||
disabled?: boolean; | ||
disabled?: boolean | undefined; | ||
} | ||
@@ -103,3 +106,3 @@ interface ItemState { | ||
} | ||
interface MachineApi<T extends PropTypes = PropTypes> { | ||
interface AccordionApi<T extends PropTypes = PropTypes> { | ||
/** | ||
@@ -128,11 +131,6 @@ * The value of the focused accordion item. | ||
declare function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): MachineApi<T>; | ||
declare function connect<T extends PropTypes>(service: Service<AccordionSchema>, normalize: NormalizeProps<T>): AccordionApi<T>; | ||
declare function machine(userContext: UserDefinedContext): _zag_js_core.Machine<MachineContext, MachineState, _zag_js_core.StateMachine.AnyEventObject>; | ||
declare const machine: _zag_js_core.MachineConfig<AccordionSchema>; | ||
declare const props: ("value" | "dir" | "id" | "getRootNode" | "multiple" | "disabled" | "orientation" | "ids" | "collapsible" | "onValueChange" | "onFocusChange")[]; | ||
declare const splitProps: <Props extends Partial<UserDefinedContext>>(props: Props) => [Partial<UserDefinedContext>, Omit<Props, "value" | "dir" | "id" | "getRootNode" | "multiple" | "disabled" | "orientation" | "ids" | "collapsible" | "onValueChange" | "onFocusChange">]; | ||
declare const itemProps: (keyof ItemProps)[]; | ||
declare const splitItemProps: <Props extends ItemProps>(props: Props) => [ItemProps, Omit<Props, keyof ItemProps>]; | ||
export { type MachineApi as Api, type UserDefinedContext as Context, type ElementIds, type FocusChangeDetails, type ItemProps, type ItemState, type Service, type ValueChangeDetails, anatomy, connect, itemProps, machine, props, splitItemProps, splitProps }; | ||
export { type AccordionApi as Api, type ElementIds, type FocusChangeDetails, type ItemProps, type ItemState, type AccordionProps as Props, type AccordionService as Service, type ValueChangeDetails, anatomy, connect, machine }; |
@@ -1,67 +0,32 @@ | ||
"use strict"; | ||
var __defProp = Object.defineProperty; | ||
var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
var __getOwnPropNames = Object.getOwnPropertyNames; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
var __export = (target, all) => { | ||
for (var name in all) | ||
__defProp(target, name, { get: all[name], enumerable: true }); | ||
}; | ||
var __copyProps = (to, from, except, desc) => { | ||
if (from && typeof from === "object" || typeof from === "function") { | ||
for (let key of __getOwnPropNames(from)) | ||
if (!__hasOwnProp.call(to, key) && key !== except) | ||
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); | ||
} | ||
return to; | ||
}; | ||
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); | ||
'use strict'; | ||
// src/index.ts | ||
var src_exports = {}; | ||
__export(src_exports, { | ||
anatomy: () => anatomy, | ||
connect: () => connect, | ||
itemProps: () => itemProps, | ||
machine: () => machine, | ||
props: () => props, | ||
splitItemProps: () => splitItemProps, | ||
splitProps: () => splitProps | ||
}); | ||
module.exports = __toCommonJS(src_exports); | ||
var anatomy$1 = require('@zag-js/anatomy'); | ||
var domQuery = require('@zag-js/dom-query'); | ||
var utils = require('@zag-js/utils'); | ||
var core = require('@zag-js/core'); | ||
// src/accordion.anatomy.ts | ||
var import_anatomy = require("@zag-js/anatomy"); | ||
var anatomy = (0, import_anatomy.createAnatomy)("accordion").parts("root", "item", "itemTrigger", "itemContent", "itemIndicator"); | ||
var anatomy = anatomy$1.createAnatomy("accordion").parts("root", "item", "itemTrigger", "itemContent", "itemIndicator"); | ||
var parts = anatomy.build(); | ||
var getRootId = (ctx) => ctx.ids?.root ?? `accordion:${ctx.id}`; | ||
var getItemId = (ctx, value) => ctx.ids?.item?.(value) ?? `accordion:${ctx.id}:item:${value}`; | ||
var getItemContentId = (ctx, value) => ctx.ids?.itemContent?.(value) ?? `accordion:${ctx.id}:content:${value}`; | ||
var getItemTriggerId = (ctx, value) => ctx.ids?.itemTrigger?.(value) ?? `accordion:${ctx.id}:trigger:${value}`; | ||
var getRootEl = (ctx) => ctx.getById(getRootId(ctx)); | ||
var getTriggerEls = (ctx) => { | ||
const ownerId = CSS.escape(getRootId(ctx)); | ||
const selector = `[aria-controls][data-ownedby='${ownerId}']:not([disabled])`; | ||
return domQuery.queryAll(getRootEl(ctx), selector); | ||
}; | ||
var getFirstTriggerEl = (ctx) => utils.first(getTriggerEls(ctx)); | ||
var getLastTriggerEl = (ctx) => utils.last(getTriggerEls(ctx)); | ||
var getNextTriggerEl = (ctx, id) => domQuery.nextById(getTriggerEls(ctx), getItemTriggerId(ctx, id)); | ||
var getPrevTriggerEl = (ctx, id) => domQuery.prevById(getTriggerEls(ctx), getItemTriggerId(ctx, id)); | ||
// src/accordion.connect.ts | ||
var import_dom_event = require("@zag-js/dom-event"); | ||
var import_dom_query2 = require("@zag-js/dom-query"); | ||
// src/accordion.dom.ts | ||
var import_dom_query = require("@zag-js/dom-query"); | ||
var import_utils = require("@zag-js/utils"); | ||
var dom = (0, import_dom_query.createScope)({ | ||
getRootId: (ctx) => ctx.ids?.root ?? `accordion:${ctx.id}`, | ||
getItemId: (ctx, value) => ctx.ids?.item?.(value) ?? `accordion:${ctx.id}:item:${value}`, | ||
getItemContentId: (ctx, value) => ctx.ids?.itemContent?.(value) ?? `accordion:${ctx.id}:content:${value}`, | ||
getItemTriggerId: (ctx, value) => ctx.ids?.itemTrigger?.(value) ?? `accordion:${ctx.id}:trigger:${value}`, | ||
getRootEl: (ctx) => dom.getById(ctx, dom.getRootId(ctx)), | ||
getTriggerEls: (ctx) => { | ||
const ownerId = CSS.escape(dom.getRootId(ctx)); | ||
const selector = `[aria-controls][data-ownedby='${ownerId}']:not([disabled])`; | ||
return (0, import_dom_query.queryAll)(dom.getRootEl(ctx), selector); | ||
}, | ||
getFirstTriggerEl: (ctx) => (0, import_utils.first)(dom.getTriggerEls(ctx)), | ||
getLastTriggerEl: (ctx) => (0, import_utils.last)(dom.getTriggerEls(ctx)), | ||
getNextTriggerEl: (ctx, id) => (0, import_dom_query.nextById)(dom.getTriggerEls(ctx), dom.getItemTriggerId(ctx, id)), | ||
getPrevTriggerEl: (ctx, id) => (0, import_dom_query.prevById)(dom.getTriggerEls(ctx), dom.getItemTriggerId(ctx, id)) | ||
}); | ||
// src/accordion.connect.ts | ||
function connect(state, send, normalize) { | ||
const focusedValue = state.context.focusedValue; | ||
const value = state.context.value; | ||
const multiple = state.context.multiple; | ||
function connect(service, normalize) { | ||
const { send, context, prop, scope, computed } = service; | ||
const focusedValue = context.get("focusedValue"); | ||
const value = context.get("value"); | ||
const multiple = prop("multiple"); | ||
function setValue(value2) { | ||
@@ -74,7 +39,7 @@ let nextValue = value2; | ||
} | ||
function getItemState(props2) { | ||
function getItemState(props) { | ||
return { | ||
expanded: value.includes(props2.value), | ||
focused: focusedValue === props2.value, | ||
disabled: Boolean(props2.disabled ?? state.context.disabled) | ||
expanded: value.includes(props.value), | ||
focused: focusedValue === props.value, | ||
disabled: Boolean(props.disabled ?? prop("disabled")) | ||
}; | ||
@@ -90,72 +55,74 @@ } | ||
...parts.root.attrs, | ||
dir: state.context.dir, | ||
id: dom.getRootId(state.context), | ||
"data-orientation": state.context.orientation | ||
dir: prop("dir"), | ||
id: getRootId(scope), | ||
"data-orientation": prop("orientation") | ||
}); | ||
}, | ||
getItemProps(props2) { | ||
const itemState = getItemState(props2); | ||
getItemProps(props) { | ||
const itemState = getItemState(props); | ||
return normalize.element({ | ||
...parts.item.attrs, | ||
dir: state.context.dir, | ||
id: dom.getItemId(state.context, props2.value), | ||
dir: prop("dir"), | ||
id: getItemId(scope, props.value), | ||
"data-state": itemState.expanded ? "open" : "closed", | ||
"data-focus": (0, import_dom_query2.dataAttr)(itemState.focused), | ||
"data-disabled": (0, import_dom_query2.dataAttr)(itemState.disabled), | ||
"data-orientation": state.context.orientation | ||
"data-focus": domQuery.dataAttr(itemState.focused), | ||
"data-disabled": domQuery.dataAttr(itemState.disabled), | ||
"data-orientation": prop("orientation") | ||
}); | ||
}, | ||
getItemContentProps(props2) { | ||
const itemState = getItemState(props2); | ||
getItemContentProps(props) { | ||
const itemState = getItemState(props); | ||
return normalize.element({ | ||
...parts.itemContent.attrs, | ||
dir: state.context.dir, | ||
dir: prop("dir"), | ||
role: "region", | ||
id: dom.getItemContentId(state.context, props2.value), | ||
"aria-labelledby": dom.getItemTriggerId(state.context, props2.value), | ||
id: getItemContentId(scope, props.value), | ||
"aria-labelledby": getItemTriggerId(scope, props.value), | ||
hidden: !itemState.expanded, | ||
"data-state": itemState.expanded ? "open" : "closed", | ||
"data-disabled": (0, import_dom_query2.dataAttr)(itemState.disabled), | ||
"data-focus": (0, import_dom_query2.dataAttr)(itemState.focused), | ||
"data-orientation": state.context.orientation | ||
"data-disabled": domQuery.dataAttr(itemState.disabled), | ||
"data-focus": domQuery.dataAttr(itemState.focused), | ||
"data-orientation": prop("orientation") | ||
}); | ||
}, | ||
getItemIndicatorProps(props2) { | ||
const itemState = getItemState(props2); | ||
getItemIndicatorProps(props) { | ||
const itemState = getItemState(props); | ||
return normalize.element({ | ||
...parts.itemIndicator.attrs, | ||
dir: state.context.dir, | ||
dir: prop("dir"), | ||
"aria-hidden": true, | ||
"data-state": itemState.expanded ? "open" : "closed", | ||
"data-disabled": (0, import_dom_query2.dataAttr)(itemState.disabled), | ||
"data-focus": (0, import_dom_query2.dataAttr)(itemState.focused), | ||
"data-orientation": state.context.orientation | ||
"data-disabled": domQuery.dataAttr(itemState.disabled), | ||
"data-focus": domQuery.dataAttr(itemState.focused), | ||
"data-orientation": prop("orientation") | ||
}); | ||
}, | ||
getItemTriggerProps(props2) { | ||
const { value: value2 } = props2; | ||
const itemState = getItemState(props2); | ||
getItemTriggerProps(props) { | ||
const { value: value2 } = props; | ||
const itemState = getItemState(props); | ||
return normalize.button({ | ||
...parts.itemTrigger.attrs, | ||
type: "button", | ||
dir: state.context.dir, | ||
id: dom.getItemTriggerId(state.context, value2), | ||
"aria-controls": dom.getItemContentId(state.context, value2), | ||
dir: prop("dir"), | ||
id: getItemTriggerId(scope, value2), | ||
"aria-controls": getItemContentId(scope, value2), | ||
"aria-expanded": itemState.expanded, | ||
disabled: itemState.disabled, | ||
"data-orientation": state.context.orientation, | ||
"data-orientation": prop("orientation"), | ||
"aria-disabled": itemState.disabled, | ||
"data-state": itemState.expanded ? "open" : "closed", | ||
"data-ownedby": dom.getRootId(state.context), | ||
"data-ownedby": getRootId(scope), | ||
onFocus() { | ||
if (itemState.disabled) return; | ||
send({ type: "TRIGGER.FOCUS", value: value2 }); | ||
queueMicrotask(() => { | ||
if (itemState.disabled) return; | ||
send({ type: "TRIGGER.FOCUS", value: value2 }); | ||
}); | ||
}, | ||
onBlur() { | ||
if (itemState.disabled) return; | ||
send("TRIGGER.BLUR"); | ||
send({ type: "TRIGGER.BLUR" }); | ||
}, | ||
onClick(event) { | ||
if (itemState.disabled) return; | ||
if ((0, import_dom_query2.isSafari)()) { | ||
if (domQuery.isSafari()) { | ||
event.currentTarget.focus(); | ||
@@ -170,15 +137,15 @@ } | ||
ArrowDown() { | ||
if (state.context.isHorizontal) return; | ||
if (computed("isHorizontal")) return; | ||
send({ type: "GOTO.NEXT", value: value2 }); | ||
}, | ||
ArrowUp() { | ||
if (state.context.isHorizontal) return; | ||
if (computed("isHorizontal")) return; | ||
send({ type: "GOTO.PREV", value: value2 }); | ||
}, | ||
ArrowRight() { | ||
if (!state.context.isHorizontal) return; | ||
if (!computed("isHorizontal")) return; | ||
send({ type: "GOTO.NEXT", value: value2 }); | ||
}, | ||
ArrowLeft() { | ||
if (!state.context.isHorizontal) return; | ||
if (!computed("isHorizontal")) return; | ||
send({ type: "GOTO.PREV", value: value2 }); | ||
@@ -193,5 +160,5 @@ }, | ||
}; | ||
const key = (0, import_dom_event.getEventKey)(event, { | ||
dir: state.context.dir, | ||
orientation: state.context.orientation | ||
const key = domQuery.getEventKey(event, { | ||
dir: prop("dir"), | ||
orientation: prop("orientation") | ||
}); | ||
@@ -208,174 +175,139 @@ const exec = keyMap[key]; | ||
} | ||
// src/accordion.machine.ts | ||
var import_core = require("@zag-js/core"); | ||
var import_utils2 = require("@zag-js/utils"); | ||
var { and, not } = import_core.guards; | ||
function machine(userContext) { | ||
const ctx = (0, import_utils2.compact)(userContext); | ||
return (0, import_core.createMachine)( | ||
{ | ||
id: "accordion", | ||
initial: "idle", | ||
context: { | ||
focusedValue: null, | ||
value: [], | ||
collapsible: false, | ||
multiple: false, | ||
orientation: "vertical", | ||
...ctx | ||
}, | ||
watch: { | ||
value: "coarseValue", | ||
multiple: "coarseValue" | ||
}, | ||
created: "coarseValue", | ||
computed: { | ||
isHorizontal: (ctx2) => ctx2.orientation === "horizontal" | ||
}, | ||
var { and, not } = core.createGuards(); | ||
var machine = core.createMachine({ | ||
props({ props }) { | ||
return { | ||
collapsible: false, | ||
multiple: false, | ||
orientation: "vertical", | ||
defaultValue: [], | ||
...props | ||
}; | ||
}, | ||
initialState() { | ||
return "idle"; | ||
}, | ||
context({ prop, bindable }) { | ||
return { | ||
focusedValue: bindable(() => ({ | ||
defaultValue: null, | ||
onChange(value) { | ||
prop("onFocusChange")?.({ value }); | ||
} | ||
})), | ||
value: bindable(() => ({ | ||
defaultValue: prop("defaultValue"), | ||
value: prop("value"), | ||
onChange(value) { | ||
prop("onValueChange")?.({ value }); | ||
} | ||
})) | ||
}; | ||
}, | ||
computed: { | ||
isHorizontal: ({ prop }) => prop("orientation") === "horizontal" | ||
}, | ||
on: { | ||
"VALUE.SET": { | ||
actions: ["setValue"] | ||
} | ||
}, | ||
states: { | ||
idle: { | ||
on: { | ||
"VALUE.SET": { | ||
actions: ["setValue"] | ||
"TRIGGER.FOCUS": { | ||
target: "focused", | ||
actions: ["setFocusedValue"] | ||
} | ||
}, | ||
states: { | ||
idle: { | ||
on: { | ||
"TRIGGER.FOCUS": { | ||
target: "focused", | ||
actions: "setFocusedValue" | ||
} | ||
} | ||
}, | ||
focused: { | ||
on: { | ||
"GOTO.NEXT": { | ||
actions: "focusNextTrigger" | ||
}, | ||
"GOTO.PREV": { | ||
actions: "focusPrevTrigger" | ||
}, | ||
"TRIGGER.CLICK": [ | ||
{ | ||
guard: and("isExpanded", "canToggle"), | ||
actions: ["collapse"] | ||
}, | ||
{ | ||
guard: not("isExpanded"), | ||
actions: ["expand"] | ||
} | ||
], | ||
"GOTO.FIRST": { | ||
actions: "focusFirstTrigger" | ||
}, | ||
"GOTO.LAST": { | ||
actions: "focusLastTrigger" | ||
}, | ||
"TRIGGER.BLUR": { | ||
target: "idle", | ||
actions: "clearFocusedValue" | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
{ | ||
guards: { | ||
canToggle: (ctx2) => !!ctx2.collapsible || !!ctx2.multiple, | ||
isExpanded: (ctx2, evt) => ctx2.value.includes(evt.value) | ||
}, | ||
actions: { | ||
collapse(ctx2, evt) { | ||
const next = ctx2.multiple ? (0, import_utils2.remove)(ctx2.value, evt.value) : []; | ||
set.value(ctx2, ctx2.multiple ? next : []); | ||
focused: { | ||
on: { | ||
"GOTO.NEXT": { | ||
actions: ["focusNextTrigger"] | ||
}, | ||
expand(ctx2, evt) { | ||
const next = ctx2.multiple ? (0, import_utils2.add)(ctx2.value, evt.value) : [evt.value]; | ||
set.value(ctx2, next); | ||
"GOTO.PREV": { | ||
actions: ["focusPrevTrigger"] | ||
}, | ||
focusFirstTrigger(ctx2) { | ||
dom.getFirstTriggerEl(ctx2)?.focus(); | ||
"TRIGGER.CLICK": [ | ||
{ | ||
guard: and("isExpanded", "canToggle"), | ||
actions: ["collapse"] | ||
}, | ||
{ | ||
guard: not("isExpanded"), | ||
actions: ["expand"] | ||
} | ||
], | ||
"GOTO.FIRST": { | ||
actions: ["focusFirstTrigger"] | ||
}, | ||
focusLastTrigger(ctx2) { | ||
dom.getLastTriggerEl(ctx2)?.focus(); | ||
"GOTO.LAST": { | ||
actions: ["focusLastTrigger"] | ||
}, | ||
focusNextTrigger(ctx2) { | ||
if (!ctx2.focusedValue) return; | ||
const triggerEl = dom.getNextTriggerEl(ctx2, ctx2.focusedValue); | ||
triggerEl?.focus(); | ||
}, | ||
focusPrevTrigger(ctx2) { | ||
if (!ctx2.focusedValue) return; | ||
const triggerEl = dom.getPrevTriggerEl(ctx2, ctx2.focusedValue); | ||
triggerEl?.focus(); | ||
}, | ||
setFocusedValue(ctx2, evt) { | ||
set.focusedValue(ctx2, evt.value); | ||
}, | ||
clearFocusedValue(ctx2) { | ||
set.focusedValue(ctx2, null); | ||
}, | ||
setValue(ctx2, evt) { | ||
set.value(ctx2, evt.value); | ||
}, | ||
coarseValue(ctx2) { | ||
if (!ctx2.multiple && ctx2.value.length > 1) { | ||
(0, import_utils2.warn)(`The value of accordion should be a single value when multiple is false.`); | ||
ctx2.value = [ctx2.value[0]]; | ||
} | ||
"TRIGGER.BLUR": { | ||
target: "idle", | ||
actions: ["clearFocusedValue"] | ||
} | ||
} | ||
} | ||
); | ||
} | ||
var invoke = { | ||
change(ctx) { | ||
ctx.onValueChange?.({ value: Array.from(ctx.value) }); | ||
}, | ||
focusChange(ctx) { | ||
ctx.onFocusChange?.({ value: ctx.focusedValue }); | ||
implementations: { | ||
guards: { | ||
canToggle: ({ prop }) => !!prop("collapsible") || !!prop("multiple"), | ||
isExpanded: ({ context }) => { | ||
const focusedValue = context.get("focusedValue"); | ||
const value = context.get("value"); | ||
return focusedValue ? value.includes(focusedValue) : false; | ||
} | ||
}, | ||
actions: { | ||
collapse({ context, prop }) { | ||
const next = prop("multiple") ? utils.remove(context.get("value"), context.get("focusedValue")) : []; | ||
context.set("value", next); | ||
}, | ||
expand({ context, prop }) { | ||
const focusedValue = context.get("focusedValue"); | ||
const next = prop("multiple") ? utils.add(context.get("value"), focusedValue) : [focusedValue]; | ||
context.set("value", next); | ||
}, | ||
focusFirstTrigger({ scope }) { | ||
getFirstTriggerEl(scope)?.focus(); | ||
}, | ||
focusLastTrigger({ scope }) { | ||
getLastTriggerEl(scope)?.focus(); | ||
}, | ||
focusNextTrigger({ context, scope }) { | ||
const focusedValue = context.get("focusedValue"); | ||
if (!focusedValue) return; | ||
const triggerEl = getNextTriggerEl(scope, focusedValue); | ||
triggerEl?.focus(); | ||
}, | ||
focusPrevTrigger({ context, scope }) { | ||
const focusedValue = context.get("focusedValue"); | ||
if (!focusedValue) return; | ||
const triggerEl = getPrevTriggerEl(scope, focusedValue); | ||
triggerEl?.focus(); | ||
}, | ||
setFocusedValue({ context, event }) { | ||
context.set("focusedValue", event.value); | ||
}, | ||
clearFocusedValue({ context }) { | ||
context.set("focusedValue", null); | ||
}, | ||
setValue({ context, event }) { | ||
context.set("value", event.value); | ||
}, | ||
coarseValue({ context, prop }) { | ||
if (!prop("multiple") && context.get("value").length > 1) { | ||
utils.warn(`The value of accordion should be a single value when multiple is false.`); | ||
context.set("value", [context.get("value")[0]]); | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
var set = { | ||
value(ctx, value) { | ||
if ((0, import_utils2.isEqual)(ctx.value, value)) return; | ||
ctx.value = value; | ||
invoke.change(ctx); | ||
}, | ||
focusedValue(ctx, value) { | ||
if ((0, import_utils2.isEqual)(ctx.focusedValue, value)) return; | ||
ctx.focusedValue = value; | ||
invoke.focusChange(ctx); | ||
} | ||
}; | ||
}); | ||
// src/accordion.props.ts | ||
var import_types = require("@zag-js/types"); | ||
var import_utils3 = require("@zag-js/utils"); | ||
var props = (0, import_types.createProps)()([ | ||
"collapsible", | ||
"dir", | ||
"disabled", | ||
"getRootNode", | ||
"id", | ||
"ids", | ||
"multiple", | ||
"onFocusChange", | ||
"onValueChange", | ||
"orientation", | ||
"value" | ||
]); | ||
var splitProps = (0, import_utils3.createSplitProps)(props); | ||
var itemProps = (0, import_types.createProps)()(["value", "disabled"]); | ||
var splitItemProps = (0, import_utils3.createSplitProps)(itemProps); | ||
// Annotate the CommonJS export names for ESM import in node: | ||
0 && (module.exports = { | ||
anatomy, | ||
connect, | ||
itemProps, | ||
machine, | ||
props, | ||
splitItemProps, | ||
splitProps | ||
}); | ||
//# sourceMappingURL=index.js.map | ||
exports.anatomy = anatomy; | ||
exports.connect = connect; | ||
exports.machine = machine; |
{ | ||
"name": "@zag-js/accordion", | ||
"version": "0.0.0-dev-20240723090825", | ||
"version": "0.0.0-v1-beta-20250220125322", | ||
"description": "Core logic for the accordion widget implemented as a state machine", | ||
@@ -20,4 +20,3 @@ "keywords": [ | ||
"files": [ | ||
"dist", | ||
"src" | ||
"dist" | ||
], | ||
@@ -31,8 +30,7 @@ "publishConfig": { | ||
"dependencies": { | ||
"@zag-js/anatomy": "0.0.0-dev-20240723090825", | ||
"@zag-js/core": "0.0.0-dev-20240723090825", | ||
"@zag-js/dom-query": "0.0.0-dev-20240723090825", | ||
"@zag-js/dom-event": "0.0.0-dev-20240723090825", | ||
"@zag-js/types": "0.0.0-dev-20240723090825", | ||
"@zag-js/utils": "0.0.0-dev-20240723090825" | ||
"@zag-js/types": "0.0.0-v1-beta-20250220125322", | ||
"@zag-js/core": "0.0.0-v1-beta-20250220125322", | ||
"@zag-js/dom-query": "0.0.0-v1-beta-20250220125322", | ||
"@zag-js/anatomy": "0.0.0-v1-beta-20250220125322", | ||
"@zag-js/utils": "0.0.0-v1-beta-20250220125322" | ||
}, | ||
@@ -56,5 +54,5 @@ "devDependencies": { | ||
"build": "tsup", | ||
"lint": "eslint src --ext .ts,.tsx", | ||
"lint": "eslint src", | ||
"typecheck": "tsc --noEmit" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
5
30839
7
730
+ Added@zag-js/anatomy@0.0.0-v1-beta-20250220125322(transitive)
+ Added@zag-js/core@0.0.0-v1-beta-20250220125322(transitive)
+ Added@zag-js/dom-query@0.0.0-v1-beta-20250220125322(transitive)
+ Added@zag-js/types@0.0.0-v1-beta-20250220125322(transitive)
+ Added@zag-js/utils@0.0.0-v1-beta-20250220125322(transitive)
- Removed@zag-js/anatomy@0.0.0-dev-20240723090825(transitive)
- Removed@zag-js/core@0.0.0-dev-20240723090825(transitive)
- Removed@zag-js/dom-event@0.0.0-dev-20240723090825(transitive)
- Removed@zag-js/dom-query@0.0.0-dev-20240723090825(transitive)
- Removed@zag-js/store@0.0.0-dev-20240723090825(transitive)
- Removed@zag-js/text-selection@0.0.0-dev-20240723090825(transitive)
- Removed@zag-js/types@0.0.0-dev-20240723090825(transitive)
- Removed@zag-js/utils@0.0.0-dev-20240723090825(transitive)
- Removedklona@2.0.6(transitive)
- Removedproxy-compare@3.0.0(transitive)