Skip to content

Commit

Permalink
refactor: color picker
Browse files Browse the repository at this point in the history
  • Loading branch information
segunadebayo committed Nov 9, 2024
1 parent da4bdce commit 87ebdd1
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changeset/cuddly-jeans-drop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@zag-js/color-picker": patch
---

- Add `--color` to the swatch and swatch trigger parts
- Allow clicking swatch being in the open state
3 changes: 3 additions & 0 deletions .xstate/color-picker.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ const fetchMachine = createMachine({
},
"EYEDROPPER.CLICK": {
actions: ["openEyeDropper"]
},
"SWATCH_TRIGGER.CLICK": {
actions: ["setValue"]
}
},
on: {
Expand Down
5 changes: 4 additions & 1 deletion packages/machines/color-picker/src/color-picker.connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,7 @@ export function connect<T extends PropTypes>(state: State, send: Send, normalize
send({ type: "SWATCH_TRIGGER.CLICK", value: swatchState.value })
},
style: {
"--color": swatchState.valueAsString,
position: "relative",
},
})
Expand All @@ -624,14 +625,16 @@ export function connect<T extends PropTypes>(state: State, send: Send, normalize
getSwatchProps(props) {
const { respectAlpha = true } = props
const swatchState = getSwatchTriggerState(props)
const color = swatchState.value.toString(respectAlpha ? "css" : "hex")
return normalize.element({
...parts.swatch.attrs,
dir: state.context.dir,
"data-state": swatchState.checked ? "checked" : "unchecked",
"data-value": swatchState.valueAsString,
style: {
"--color": color,
position: "relative",
background: swatchState.value.toString(respectAlpha ? "css" : "hex"),
background: color,
},
})
},
Expand Down
3 changes: 3 additions & 0 deletions packages/machines/color-picker/src/color-picker.machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ export function machine(userContext: UserDefinedContext) {
"EYEDROPPER.CLICK": {
actions: ["openEyeDropper"],
},
"SWATCH_TRIGGER.CLICK": {
actions: ["setValue"],
},
},

states: {
Expand Down

0 comments on commit 87ebdd1

Please sign in to comment.