Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(file-upload): fix reopening the system file picker on browsers other than Chrome #869

Merged
merged 3 commits into from
Sep 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1096,7 +1096,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
Loading