Skip to content

Commit

Permalink
fix(file-upload): fix reopening the system file picker on browsers ot…
Browse files Browse the repository at this point in the history
…her than Chrome (#869)
  • Loading branch information
srflp authored Sep 30, 2023
1 parent 0aff6bf commit bf574e5
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 38 deletions.
6 changes: 6 additions & 0 deletions .changeset/unlucky-carrots-punch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@zag-js/file-upload": patch
"@zag-js/docs": patch
---

Fix reopening the system file picker in file-upload on browsers other than Chrome
33 changes: 20 additions & 13 deletions .xstate/file-upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const fetchMachine = createMachine({
id: "fileupload",
initial: "idle",
context: {
"!isWithinRange": false,
"!isWithinRange": false
},
on: {
Expand All @@ -31,8 +32,12 @@ const fetchMachine = createMachine({
states: {
idle: {
on: {
OPEN: "open",
"DROPZONE.CLICK": "open",
OPEN: {
actions: ["openFilePicker"]
},
"DROPZONE.CLICK": {
actions: ["openFilePicker"]
},
"DROPZONE.FOCUS": "focused",
"DROPZONE.DRAG_OVER": [{
cond: "!isWithinRange",
Expand All @@ -45,10 +50,19 @@ const fetchMachine = createMachine({
},
focused: {
on: {
OPEN: "open",
"DROPZONE.CLICK": "open",
"DROPZONE.ENTER": "open",
"DROPZONE.BLUR": "idle"
OPEN: {
actions: ["openFilePicker"]
},
"DROPZONE.CLICK": {
actions: ["openFilePicker"]
},
"DROPZONE.DRAG_OVER": [{
cond: "!isWithinRange",
target: "dragging",
actions: ["setInvalid"]
}, {
target: "dragging"
}]
}
},
dragging: {
Expand All @@ -62,13 +76,6 @@ const fetchMachine = createMachine({
actions: ["clearInvalid"]
}
}
},
open: {
activities: ["trackWindowFocus"],
entry: ["openFilePicker"],
on: {
CLOSE: "idle"
}
}
}
}, {
Expand Down
2 changes: 1 addition & 1 deletion packages/docs/api.json
Original file line number Diff line number Diff line change
Expand Up @@ -1108,7 +1108,7 @@
},
"isFocused": {
"type": "boolean",
"description": "Whether the user is focused on the root element"
"description": "Whether the user is focused on the dropzone element"
},
"open": {
"type": "() => void",
Expand Down
43 changes: 20 additions & 23 deletions packages/machines/file-upload/src/file-upload.machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,12 @@ export function machine(userContext: UserDefinedContext) {
states: {
idle: {
on: {
OPEN: "open",
"DROPZONE.CLICK": "open",
OPEN: {
actions: ["openFilePicker"],
},
"DROPZONE.CLICK": {
actions: ["openFilePicker"],
},
"DROPZONE.FOCUS": "focused",
"DROPZONE.DRAG_OVER": [
{
Expand All @@ -53,10 +57,20 @@ export function machine(userContext: UserDefinedContext) {
},
focused: {
on: {
OPEN: "open",
"DROPZONE.CLICK": "open",
"DROPZONE.ENTER": "open",
"DROPZONE.BLUR": "idle",
OPEN: {
actions: ["openFilePicker"],
},
"DROPZONE.CLICK": {
actions: ["openFilePicker"],
},
"DROPZONE.DRAG_OVER": [
{
guard: not("isWithinRange"),
target: "dragging",
actions: ["setInvalid"],
},
{ target: "dragging" },
],
},
},
dragging: {
Expand All @@ -71,29 +85,12 @@ export function machine(userContext: UserDefinedContext) {
},
},
},
open: {
activities: ["trackWindowFocus"],
entry: ["openFilePicker"],
on: {
CLOSE: "idle",
},
},
},
},
{
guards: {
isWithinRange: (ctx, evt) => isFilesWithinRange(ctx, evt.count),
},
activities: {
trackWindowFocus(ctx, _evt, { send }) {
const win = dom.getWin(ctx)
const onWindowFocus = () => {
raf(() => send("CLOSE"))
}
win.addEventListener("focus", onWindowFocus)
return () => win.removeEventListener("focus", onWindowFocus)
},
},
actions: {
openFilePicker(ctx) {
raf(() => {
Expand Down
2 changes: 1 addition & 1 deletion packages/machines/file-upload/src/file-upload.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export interface MachineApi<T extends PropTypes> {
*/
isDragging: boolean
/**
* Whether the user is focused on the root element
* Whether the user is focused on the dropzone element
*/
isFocused: boolean
/**
Expand Down

4 comments on commit bf574e5

@vercel
Copy link

@vercel vercel bot commented on bf574e5 Sep 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

zag-nextjs – ./examples/next-ts

zag-nextjs-chakra-ui.vercel.app
zag-two.vercel.app
zag-nextjs-git-main-chakra-ui.vercel.app

@vercel
Copy link

@vercel vercel bot commented on bf574e5 Sep 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

zag-vue – ./examples/vue-ts

zag-vue-git-main-chakra-ui.vercel.app
zag-vue-chakra-ui.vercel.app
zag-vue.vercel.app

@vercel
Copy link

@vercel vercel bot commented on bf574e5 Sep 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

zag-solid – ./examples/solid-ts

zag-solid-chakra-ui.vercel.app
zag-solid.vercel.app
zag-solid-git-main-chakra-ui.vercel.app

@vercel
Copy link

@vercel vercel bot commented on bf574e5 Sep 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.