Skip to content

Commit

Permalink
feat: expose machine context props for better dx
Browse files Browse the repository at this point in the history
  • Loading branch information
segunadebayo committed Feb 18, 2024
1 parent 4a7d739 commit 2a024fb
Show file tree
Hide file tree
Showing 70 changed files with 846 additions and 42 deletions.
10 changes: 10 additions & 0 deletions .changeset/lucky-boxes-vanish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"@zag-js/accordion": minor
"@zag-js/clipboard": minor
"@zag-js/carousel": minor
"@zag-js/checkbox": minor
"@zag-js/avatar": minor
"@zag-js/types": minor
---

Expose all machine context properties as array to improve DX of building design system components.
5 changes: 5 additions & 0 deletions .changeset/pink-apes-applaud.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@zag-js/splitter": patch
---

Remove support for `onSizeChangeStart`
6 changes: 6 additions & 0 deletions .changeset/plenty-boxes-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@zag-js/menu": minor
---

- Rename `GroupProps` to `ItemGroupProps`
- Rename `LabelProps` to `ItemGroupLabelProps`
9 changes: 3 additions & 6 deletions .xstate/splitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,15 @@ const fetchMachine = createMachine({
on: {
POINTER_DOWN: {
target: "dragging",
actions: ["setActiveHandleId", "invokeOnResizeStart"]
actions: ["setActiveHandleId"]
},
POINTER_LEAVE: "idle"
}
},
hover: {
tags: ["focus"],
on: {
POINTER_DOWN: {
target: "dragging",
actions: ["invokeOnResizeStart"]
},
POINTER_DOWN: "dragging",
POINTER_LEAVE: "idle"
}
},
Expand All @@ -74,7 +71,7 @@ const fetchMachine = createMachine({
BLUR: "idle",
POINTER_DOWN: {
target: "dragging",
actions: ["setActiveHandleId", "invokeOnResizeStart"]
actions: ["setActiveHandleId"]
},
ARROW_LEFT: {
cond: "isHorizontal",
Expand Down
18 changes: 18 additions & 0 deletions packages/machines/accordion/src/accordion.props.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { createProps } from "@zag-js/types"
import type { ItemProps, UserDefinedContext } from "./accordion.types"

export const props = createProps<UserDefinedContext>()([
"collapsible",
"dir",
"disabled",
"getRootNode",
"id",
"ids",
"multiple",
"onFocusChange",
"onValueChange",
"orientation",
"value",
])

export const itemProps = createProps<ItemProps>()(["value", "disabled"])
1 change: 1 addition & 0 deletions packages/machines/accordion/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export { anatomy } from "./accordion.anatomy"
export { connect } from "./accordion.connect"
export { machine } from "./accordion.machine"
export * from "./accordion.props"
export type {
MachineApi as Api,
UserDefinedContext as Context,
Expand Down
4 changes: 4 additions & 0 deletions packages/machines/avatar/src/avatar.props.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { createProps } from "@zag-js/types"
import type { UserDefinedContext } from "./avatar.types"

export const props = createProps<UserDefinedContext>()(["dir", "id", "onLoadingStatusChange", "getRootNode"])
3 changes: 2 additions & 1 deletion packages/machines/avatar/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { anatomy } from "./avatar.anatomy"
export { connect } from "./avatar.connect"
export { machine } from "./avatar.machine"
export type { UserDefinedContext as Context, MachineApi as Api, StatusChangeDetails } from "./avatar.types"
export * from "./avatar.props"
export type { MachineApi as Api, UserDefinedContext as Context, StatusChangeDetails } from "./avatar.types"
6 changes: 3 additions & 3 deletions packages/machines/carousel/src/carousel.connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function connect<T extends PropTypes>(state: State, send: Send, normalize
const activeSnap = state.context.scrollSnaps[state.context.index]
const slidesInView = isDom() ? getSlidesInView(state.context)(activeSnap) : []

function getItemStateState(props: ItemProps): ItemState {
function getItemState(props: ItemProps): ItemState {
const { index } = props
return {
valueText: `Slide ${index + 1}`,
Expand Down Expand Up @@ -44,7 +44,7 @@ export function connect<T extends PropTypes>(state: State, send: Send, normalize
send("PREV")
},

getItemState: getItemStateState,
getItemState: getItemState,

play() {
send("PLAY")
Expand Down Expand Up @@ -95,7 +95,7 @@ export function connect<T extends PropTypes>(state: State, send: Send, normalize

getItemProps(props) {
const { index } = props
const sliderState = getItemStateState(props)
const sliderState = getItemState(props)

return normalize.element({
...parts.item.attrs,
Expand Down
18 changes: 18 additions & 0 deletions packages/machines/carousel/src/carousel.props.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { createProps } from "@zag-js/types"
import type { IndicatorProps, UserDefinedContext } from "./carousel.types"

export const props = createProps<UserDefinedContext>()([
"align",
"dir",
"getRootNode",
"id",
"ids",
"index",
"loop",
"onIndexChange",
"orientation",
"slidesPerView",
"spacing",
])

export const indicatorProps = createProps<IndicatorProps>()(["index", "readOnly"])
1 change: 1 addition & 0 deletions packages/machines/carousel/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export { anatomy } from "./carousel.anatomy"
export { connect } from "./carousel.connect"
export { machine } from "./carousel.machine"
export * from "./carousel.props"
export type {
MachineApi as Api,
UserDefinedContext as Context,
Expand Down
17 changes: 17 additions & 0 deletions packages/machines/checkbox/src/checkbox.props.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { createProps } from "@zag-js/types"
import type { UserDefinedContext } from "./checkbox.types"

export const props = createProps<UserDefinedContext>()([
"checked",
"dir",
"disabled",
"form",
"getRootNode",
"id",
"ids",
"invalid",
"name",
"onCheckedChange",
"required",
"value",
])
1 change: 1 addition & 0 deletions packages/machines/checkbox/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export { anatomy } from "./checkbox.anatomy"
export { connect } from "./checkbox.connect"
export { machine } from "./checkbox.machine"
export * from "./checkbox.props"
export type {
MachineApi as Api,
CheckedChangeDetails,
Expand Down
13 changes: 13 additions & 0 deletions packages/machines/clipboard/src/clipboard.props.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { createProps } from "@zag-js/types"
import type { IndicatorProps, UserDefinedContext } from "./clipboard.types"

export const props = createProps<UserDefinedContext>()([
"getRootNode",
"id",
"ids",
"value",
"timeout",
"onCopyStatusChange",
])

export const indicatorProps = createProps<IndicatorProps>()(["copied"])
3 changes: 2 additions & 1 deletion packages/machines/clipboard/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
export { anatomy } from "./clipboard.anatomy"
export { connect } from "./clipboard.connect"
export { machine } from "./clipboard.machine"
export * from "./clipboard.props"
export type {
MachineApi as Api,
UserDefinedContext as Context,
ElementIds,
CopyStatusDetails,
ElementIds,
IndicatorProps,
} from "./clipboard.types"
13 changes: 13 additions & 0 deletions packages/machines/collapsible/src/collapsible.props.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { createProps } from "@zag-js/types"
import type { UserDefinedContext } from "./collapsible.types"

export const props = createProps<UserDefinedContext>()([
"dir",
"disabled",
"getRootNode",
"id",
"ids",
"onOpenChange",
"open.controlled",
"open",
])
44 changes: 44 additions & 0 deletions packages/machines/color-picker/src/color-picker.props.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { createProps } from "@zag-js/types"
import type {
AreaProps,
ChannelProps,
SwatchProps,
SwatchTriggerProps,
TransparencyGridProps,
UserDefinedContext,
} from "./color-picker.types"

export const props = createProps<UserDefinedContext>()([
"closeOnSelect",
"dir",
"disabled",
"format",
"getRootNode",
"id",
"ids",
"initialFocusEl",
"name",
"name",
"onFocusOutside",
"onFormatChange",
"onInteractOutside",
"onOpenChange",
"onPointerDownOutside",
"onValueChange",
"onValueChangeEnd",
"open.controlled",
"open",
"positioning",
"readOnly",
"value",
])

export const areaProps = createProps<AreaProps>()(["xChannel", "yChannel"])

export const channelProps = createProps<ChannelProps>()(["channel", "orientation"])

export const swatchTriggerProps = createProps<SwatchTriggerProps>()(["value", "disabled"])

export const swatchProps = createProps<SwatchProps>()(["value", "respectAlpha"])

export const transparencyGridProps = createProps<TransparencyGridProps>()(["size"])
44 changes: 44 additions & 0 deletions packages/machines/combobox/src/combobox.props.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { createProps } from "@zag-js/types"
import type { ItemGroupLabelProps, ItemGroupProps, ItemProps, UserDefinedContext } from "./combobox.types"

export const props = createProps<UserDefinedContext>()([
"allowCustomValue",
"autoFocus",
"closeOnSelect",
"dir",
"disabled",
"form",
"getRootNode",
"highlightedValue",
"id",
"ids",
"inputBehavior",
"collection",
"inputValue",
"invalid",
"loop",
"multiple",
"name",
"onFocusOutside",
"onHighlightChange",
"onInputValueChange",
"onInteractOutside",
"onOpenChange",
"onOpenChange",
"onPointerDownOutside",
"onValueChange",
"openOnClick",
"placeholder",
"positioning",
"readOnly",
"selectionBehavior",
"selectOnBlur",
"translations",
"value",
])

export const itemGroupLabelProps = createProps<ItemGroupLabelProps>()(["htmlFor"])

export const itemGroupProps = createProps<ItemGroupProps>()(["id"])

export const itemProps = createProps<ItemProps>()(["item"])
54 changes: 54 additions & 0 deletions packages/machines/date-picker/src/date-picker.props.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { createProps } from "@zag-js/types"
import type {
InputProps,
PresetTriggerProps,
TableCellProps,
TableProps,
UserDefinedContext,
ViewProps,
} from "./date-picker.types"

export const props = createProps<UserDefinedContext>()([
"closeOnSelect",
"dir",
"disabled",
"fixedWeeks",
"focusedValue",
"format",
"getRootNode",
"id",
"ids",
"isDateUnavailable",
"isDateUnavailable",
"locale",
"max",
"min",
"modal",
"name",
"numOfMonths",
"onFocusChange",
"onOpenChange",
"onValueChange",
"onViewChange",
"open",
"open.controlled",
"parse",
"positioning",
"readOnly",
"selectionMode",
"startOfWeek",
"timeZone",
"translations",
"value",
"view",
])

export const inputProps = createProps<InputProps>()(["index"])

export const presetTriggerProps = createProps<PresetTriggerProps>()(["value"])

export const tableProps = createProps<TableProps>()(["columns", "id", "view"])

export const tableCellProps = createProps<TableCellProps>()(["disabled", "value", "columns"])

export const viewProps = createProps<ViewProps>()(["view"])
1 change: 1 addition & 0 deletions packages/machines/date-picker/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export { anatomy } from "./date-picker.anatomy"
export { connect } from "./date-picker.connect"
export { machine } from "./date-picker.machine"
export { parse } from "./date-picker.parse"
export * from "./date-picker.props"
export type {
MachineApi as Api,
Calendar,
Expand Down
28 changes: 28 additions & 0 deletions packages/machines/dialog/src/dialog.props.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { createProps } from "@zag-js/types"
import type { UserDefinedContext } from "./dialog.types"

export const props = createProps<UserDefinedContext>()([
"aria-label",
"closeOnEscapeKeyDown",
"closeOnInteractOutside",
"dir",
"finalFocusEl",
"getRootNode",
"getRootNode",
"id",
"id",
"ids",
"initialFocusEl",
"modal",
"onEscapeKeyDown",
"onFocusOutside",
"onInteractOutside",
"onOpenChange",
"onPointerDownOutside",
"open.controlled",
"open",
"preventScroll",
"restoreFocus",
"role",
"trapFocus",
])
Loading

0 comments on commit 2a024fb

Please sign in to comment.