Skip to content

Commit

Permalink
fix(file-upload): fix opening the system file picker multiple times o…
Browse files Browse the repository at this point in the history
…n browsers other than Chrome
  • Loading branch information
srflp committed Sep 13, 2023
1 parent 488b2ed commit 15f9daa
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 36 deletions.
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
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

0 comments on commit 15f9daa

Please sign in to comment.