Skip to content

Commit

Permalink
refactor: composite widgets (#1489)
Browse files Browse the repository at this point in the history
* refactor: svelte

* chore: rename

* chore: rename

* refactor: composite widgets

* fix: menu composite

* chore: add changeset

* refactor: code

* chore: changeset

* refactor: combobox and tabs composition

* fix: dialog + initial focus

* chore: slider tooltip composition

* feat(slider): invoke on change end of keyboard

* chore: reduce pkgs

* docs: update

* chore: refactor

* chore: refactor

* docs: update changeset
  • Loading branch information
segunadebayo authored May 14, 2024
1 parent 79498c3 commit 6784564
Show file tree
Hide file tree
Showing 137 changed files with 1,573 additions and 2,828 deletions.
5 changes: 5 additions & 0 deletions .changeset/curly-snails-marry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@zag-js/combobox": minor
---

Rename `triggerProps` to `getTriggerProps()` to allow for more flexible compositions.
5 changes: 5 additions & 0 deletions .changeset/nervous-kids-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@zag-js/popper": patch
---

Fix issue where positioner does not respect the `offset.crossAxis`
7 changes: 7 additions & 0 deletions .changeset/odd-moles-fetch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@zag-js/dialog": minor
---

- Prevent closing dialog on outside click when `role=alertdialog` is set.
- Set the initial focus to the close trigger, when `role=alertdialog` is set to prevent accidental selection of
destructive action.
5 changes: 5 additions & 0 deletions .changeset/twelve-dragons-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@zag-js/slider": minor
---

Invoke `onValueChangeEnd` when using keyboard to interact with slider thumb
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
strict-peer-dependencies=false
package-manager-strict=false
71 changes: 37 additions & 34 deletions .xstate/combobox.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@ const fetchMachine = createMachine({
},
"TRIGGER.CLICK": [{
cond: "isOpenControlled",
actions: ["focusInput", "highlightFirstSelectedItem", "invokeOnOpen"]
actions: ["setInitialFocus", "highlightFirstSelectedItem", "invokeOnOpen"]
}, {
target: "interacting",
actions: ["focusInput", "highlightFirstSelectedItem", "invokeOnOpen"]
actions: ["setInitialFocus", "highlightFirstSelectedItem", "invokeOnOpen"]
}],
"INPUT.CLICK": [{
cond: "isOpenControlled",
actions: ["invokeOnOpen"]
actions: ["highlightFirstSelectedItem", "invokeOnOpen"]
}, {
target: "interacting",
actions: ["highlightFirstSelectedItem", "invokeOnOpen"]
Expand All @@ -127,7 +127,7 @@ const fetchMachine = createMachine({
},
focused: {
tags: ["focused", "closed"],
entry: ["focusInputOrTrigger", "scrollContentToTop", "clearHighlightedItem"],
entry: ["scrollContentToTop", "clearHighlightedItem"],
on: {
"CONTROLLED.OPEN": [{
cond: "isChangeEvent",
Expand All @@ -137,11 +137,11 @@ const fetchMachine = createMachine({
}],
"INPUT.CHANGE": [{
cond: "isOpenControlled && openOnChange",
actions: ["setInputValue", "invokeOnOpen"]
actions: ["setInputValue", "invokeOnOpen", "highlightFirstItemIfNeeded"]
}, {
cond: "openOnChange",
target: "suggesting",
actions: ["setInputValue", "invokeOnOpen"]
actions: ["setInputValue", "invokeOnOpen", "highlightFirstItemIfNeeded"]
}, {
actions: "setInputValue"
}],
Expand All @@ -164,10 +164,10 @@ const fetchMachine = createMachine({
}],
"TRIGGER.CLICK": [{
cond: "isOpenControlled",
actions: ["focusInput", "highlightFirstSelectedItem", "invokeOnOpen"]
actions: ["setInitialFocus", "highlightFirstSelectedItem", "invokeOnOpen"]
}, {
target: "interacting",
actions: ["focusInput", "highlightFirstSelectedItem", "invokeOnOpen"]
actions: ["setInitialFocus", "highlightFirstSelectedItem", "invokeOnOpen"]
}],
"INPUT.ARROW_DOWN": [
// == group 1 ==
Expand Down Expand Up @@ -220,11 +220,13 @@ const fetchMachine = createMachine({
},
interacting: {
tags: ["open", "focused"],
activities: ["scrollIntoView", "trackDismissableLayer", "computePlacement", "hideOtherElements", "trackContentHeight"],
entry: ["setInitialFocus"],
activities: ["scrollToHighlightedItem", "trackDismissableLayer", "computePlacement", "hideOtherElements"],
on: {
"CONTROLLED.CLOSE": [{
cond: "restoreFocus",
target: "focused"
target: "focused",
actions: ["setFinalFocus"]
}, {
target: "idle"
}],
Expand Down Expand Up @@ -252,7 +254,7 @@ const fetchMachine = createMachine({
}, {
cond: "closeOnSelect",
target: "focused",
actions: ["selectHighlightedItem", "invokeOnClose"]
actions: ["selectHighlightedItem", "invokeOnClose", "setFinalFocus"]
}, {
actions: ["selectHighlightedItem"]
}],
Expand All @@ -276,7 +278,7 @@ const fetchMachine = createMachine({
}, {
cond: "closeOnSelect",
target: "focused",
actions: ["selectItem", "invokeOnClose"]
actions: ["selectItem", "invokeOnClose", "setFinalFocus"]
}, {
actions: ["selectItem"]
}],
Expand All @@ -292,7 +294,7 @@ const fetchMachine = createMachine({
actions: "invokeOnClose"
}, {
target: "focused",
actions: ["invokeOnClose"]
actions: ["invokeOnClose", "setFinalFocus"]
}],
"TRIGGER.CLICK": [{
cond: "isOpenControlled",
Expand Down Expand Up @@ -321,28 +323,29 @@ const fetchMachine = createMachine({
}],
CLOSE: [{
cond: "isOpenControlled",
actions: "invokeOnClose"
actions: ["invokeOnClose"]
}, {
target: "focused",
actions: "invokeOnClose"
actions: ["invokeOnClose", "setFinalFocus"]
}],
"VALUE.CLEAR": [{
cond: "isOpenControlled",
actions: ["clearInputValue", "clearSelectedItems", "invokeOnClose"]
}, {
target: "focused",
actions: ["clearInputValue", "clearSelectedItems", "invokeOnClose"]
actions: ["clearInputValue", "clearSelectedItems", "invokeOnClose", "setFinalFocus"]
}]
}
},
suggesting: {
tags: ["open", "focused"],
activities: ["trackDismissableLayer", "scrollIntoView", "computePlacement", "trackChildNodes", "hideOtherElements", "trackContentHeight"],
entry: ["focusInput"],
activities: ["trackDismissableLayer", "scrollToHighlightedItem", "computePlacement", "trackChildNodes", "hideOtherElements"],
entry: ["setInitialFocus"],
on: {
"CONTROLLED.CLOSE": [{
cond: "restoreFocus",
target: "focused"
target: "focused",
actions: ["setFinalFocus"]
}, {
target: "idle"
}],
Expand All @@ -351,11 +354,11 @@ const fetchMachine = createMachine({
},
"INPUT.ARROW_DOWN": {
target: "interacting",
actions: "highlightNextItem"
actions: ["highlightNextItem"]
},
"INPUT.ARROW_UP": {
target: "interacting",
actions: "highlightPrevItem"
actions: ["highlightPrevItem"]
},
"INPUT.HOME": {
target: "interacting",
Expand All @@ -371,7 +374,7 @@ const fetchMachine = createMachine({
}, {
cond: "closeOnSelect",
target: "focused",
actions: ["selectHighlightedItem", "invokeOnClose"]
actions: ["selectHighlightedItem", "invokeOnClose", "setFinalFocus"]
}, {
actions: ["selectHighlightedItem"]
}],
Expand All @@ -383,17 +386,17 @@ const fetchMachine = createMachine({
}],
"LAYER.ESCAPE": [{
cond: "isOpenControlled",
actions: "invokeOnClose"
actions: ["invokeOnClose"]
}, {
target: "focused",
actions: "invokeOnClose"
actions: ["invokeOnClose"]
}],
"ITEM.POINTER_MOVE": {
target: "interacting",
actions: "setHighlightedItem"
actions: ["setHighlightedItem"]
},
"ITEM.POINTER_LEAVE": {
actions: "clearHighlightedItem"
actions: ["clearHighlightedItem"]
},
"LAYER.INTERACT_OUTSIDE": [
// == group 1 ==
Expand All @@ -408,41 +411,41 @@ const fetchMachine = createMachine({
// == group 2 ==
{
cond: "isOpenControlled",
actions: "invokeOnClose"
actions: ["invokeOnClose"]
}, {
target: "idle",
actions: "invokeOnClose"
actions: ["invokeOnClose"]
}],
"TRIGGER.CLICK": [{
cond: "isOpenControlled",
actions: "invokeOnClose"
actions: ["invokeOnClose"]
}, {
target: "focused",
actions: "invokeOnClose"
actions: ["invokeOnClose"]
}],
"ITEM.CLICK": [{
cond: "isOpenControlled && closeOnSelect",
actions: ["selectItem", "invokeOnClose"]
}, {
cond: "closeOnSelect",
target: "focused",
actions: ["selectItem", "invokeOnClose"]
actions: ["selectItem", "invokeOnClose", "setFinalFocus"]
}, {
actions: ["selectItem"]
}],
CLOSE: [{
cond: "isOpenControlled",
actions: "invokeOnClose"
actions: ["invokeOnClose"]
}, {
target: "focused",
actions: "invokeOnClose"
actions: ["invokeOnClose", "setFinalFocus"]
}],
"VALUE.CLEAR": [{
cond: "isOpenControlled",
actions: ["clearInputValue", "clearSelectedItems", "invokeOnClose"]
}, {
target: "focused",
actions: ["clearInputValue", "clearSelectedItems", "invokeOnClose"]
actions: ["clearInputValue", "clearSelectedItems", "invokeOnClose", "setFinalFocus"]
}]
}
}
Expand Down
6 changes: 3 additions & 3 deletions .xstate/dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,21 @@ const fetchMachine = createMachine({
on: {
"CONTROLLED.CLOSE": {
target: "closed",
actions: ["restoreFocus"]
actions: ["setFinalFocus"]
},
CLOSE: [{
cond: "isOpenControlled",
actions: ["invokeOnClose"]
}, {
target: "closed",
actions: ["invokeOnClose", "restoreFocus"]
actions: ["invokeOnClose", "setFinalFocus"]
}],
TOGGLE: [{
cond: "isOpenControlled",
actions: ["invokeOnClose"]
}, {
target: "closed",
actions: ["invokeOnClose", "restoreFocus"]
actions: ["invokeOnClose", "setFinalFocus"]
}]
}
},
Expand Down
8 changes: 0 additions & 8 deletions .xstate/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ const fetchMachine = createMachine({
"isArrowLeftEvent": false,
"!isTriggerItem && isOpenControlled": false,
"!isTriggerItem": false,
"isForwardTabNavigation": false,
"isSubmenu && isOpenControlled": false,
"isSubmenu": false,
"isTriggerItemHighlighted": false,
Expand Down Expand Up @@ -284,12 +283,6 @@ const fetchMachine = createMachine({
target: "closed",
actions: "invokeOnClose"
}],
TAB: [{
cond: "isForwardTabNavigation",
actions: ["highlightNextItem"]
}, {
actions: ["highlightPrevItem"]
}],
ARROW_UP: {
actions: ["highlightPrevItem", "focusMenu"]
},
Expand Down Expand Up @@ -400,7 +393,6 @@ const fetchMachine = createMachine({
"isArrowLeftEvent": ctx => ctx["isArrowLeftEvent"],
"!isTriggerItem && isOpenControlled": ctx => ctx["!isTriggerItem && isOpenControlled"],
"!isTriggerItem": ctx => ctx["!isTriggerItem"],
"isForwardTabNavigation": ctx => ctx["isForwardTabNavigation"],
"isSubmenu && isOpenControlled": ctx => ctx["isSubmenu && isOpenControlled"],
"isTriggerItemHighlighted": ctx => ctx["isTriggerItemHighlighted"],
"closeOnSelect && isOpenControlled": ctx => ctx["closeOnSelect && isOpenControlled"],
Expand Down
4 changes: 2 additions & 2 deletions .xstate/popover.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ const fetchMachine = createMachine({
on: {
"CONTROLLED.CLOSE": {
target: "closed",
actions: ["restoreFocus"]
actions: ["setFinalFocus"]
},
CLOSE: [{
cond: "isOpenControlled",
actions: ["invokeOnClose"]
}, {
target: "closed",
actions: ["invokeOnClose", "restoreFocus"]
actions: ["invokeOnClose", "setFinalFocus"]
}],
TOGGLE: [{
cond: "isOpenControlled",
Expand Down
4 changes: 2 additions & 2 deletions .xstate/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ const fetchMachine = createMachine({
},
focused: {
tags: ["closed"],
entry: ["focusTriggerEl"],
entry: ["setFinalFocus"],
on: {
"CONTROLLED.OPEN": [{
cond: "isTriggerClickEvent",
Expand Down Expand Up @@ -188,7 +188,7 @@ const fetchMachine = createMachine({
},
open: {
tags: ["open"],
entry: ["focusContentEl"],
entry: ["setInitialFocus"],
exit: ["scrollContentToTop"],
activities: ["trackDismissableElement", "computePlacement", "scrollToHighlightedItem"],
on: {
Expand Down
Loading

0 comments on commit 6784564

Please sign in to comment.