@greenharness/greenrack-ui 7.0.2 → 7.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@greenharness/greenrack-ui",
3
- "version": "7.0.2",
3
+ "version": "7.1.1",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -92,7 +92,7 @@
92
92
  "dependencies": {
93
93
  "@tanstack/react-virtual": "^3.14.13",
94
94
  "qrcode": "^1.5.4",
95
- "@greenharness/greenrack-tokens": "^7.0.2"
95
+ "@greenharness/greenrack-tokens": "^7.1.1"
96
96
  },
97
97
  "devDependencies": {
98
98
  "@babel/preset-typescript": "^7.28.0",
@@ -102,7 +102,7 @@
102
102
  "@types/react": "^19.0.0",
103
103
  "@types/react-dom": "^19.0.0",
104
104
  "@vitejs/plugin-react": "^4.0.0",
105
- "effect": "^3.0.0",
105
+ "effect": "^3.22.5",
106
106
  "react-hook-form": "^7.55.0",
107
107
  "react-native": "0.81.5",
108
108
  "react-native-gesture-handler": "2.28.0",
@@ -29,6 +29,8 @@ interface ConfirmDialogProps {
29
29
  onConfirm?: () => void
30
30
  confirmLabel?: ReactNode
31
31
  size?: DialogSize
32
+ /** Called once per finished exit, after focus is back; see Dialog.Root `onExitComplete`. */
33
+ onExitComplete?: () => void
32
34
  }
33
35
 
34
36
  /**
@@ -49,6 +51,7 @@ export function ConfirmDialog({
49
51
  onConfirm,
50
52
  confirmLabel = 'Confirm',
51
53
  size = 'sm',
54
+ onExitComplete,
52
55
  }: ConfirmDialogProps) {
53
56
  const [typed, setTyped] = useState('')
54
57
  const satisfied = !confirmPhrase || typed.trim() === confirmPhrase
@@ -59,7 +62,7 @@ export function ConfirmDialog({
59
62
  }
60
63
 
61
64
  return (
62
- <Dialog.Root open={open} onOpenChange={handleOpenChange}>
65
+ <Dialog.Root open={open} onOpenChange={handleOpenChange} onExitComplete={onExitComplete}>
63
66
  <Dialog.Portal size={size}>
64
67
  <Dialog.Header>
65
68
  <Dialog.Title>{title}</Dialog.Title>
@@ -2,7 +2,7 @@ import type {ComponentMeta} from '../component-meta'
2
2
 
3
3
  export const meta: ComponentMeta = {
4
4
  description:
5
- 'Modal dialog with backdrop overlay. Centered on screen with scale+fade animation. Moves focus in on open (initialFocus, else the first focusable element, else the dialog) and back on close; dismisses on backdrop click (dismissable) or Escape (closeOnEscape, independent of dismissable). Use for confirmations, forms, or content that requires user attention before continuing.',
5
+ 'Modal dialog with backdrop overlay. Centered on screen with scale+fade animation. Moves focus in on open (initialFocus, else the first focusable element, else the dialog) and back on close; dismisses on backdrop click (dismissable) or Escape (closeOnEscape, independent of dismissable). `onExitComplete` is called once per finished exit after focus is back, the place to unmount: for an inside close (Escape, backdrop, Dialog.Close) `onOpenChange(false)` runs first, then `onExitComplete`; an outside close (controlled open turning false) calls only `onExitComplete`; it is not called for an exit cancelled by a reopen; an owner that unmounts on `onOpenChange(false)` instead loses the focus hand-back; on native it runs after the accessibility-focus request is sent (delivery not confirmed). Use for confirmations, forms, or content that requires user attention before continuing.',
6
6
  whenToUse: [
7
7
  'Confirmation prompts (delete, revoke, destructive actions)',
8
8
  'Short forms or data entry that blocks the main flow',
@@ -82,6 +82,17 @@ interface RootProps {
82
82
  /** Web only: on native, screen-reader focus moves to the dialog itself. */
83
83
  initialFocus?: RefObject<HTMLElement | null>
84
84
  closeAnimationDuration?: DurationToken
85
+ /**
86
+ * Called once per finished exit, after focus is back on what held it before
87
+ * the open: the place to unmount. For an inside close (Escape, backdrop,
88
+ * Dialog.Close) `onOpenChange(false)` runs first, then `onExitComplete`; an
89
+ * outside close (a controlled `open` turning false) calls only
90
+ * `onExitComplete`. Not called for an exit cancelled by a reopen. An owner
91
+ * that unmounts on `onOpenChange(false)` instead loses the focus hand-back.
92
+ * On native it runs after the accessibility-focus request is sent (delivery
93
+ * is not confirmed).
94
+ */
95
+ onExitComplete?: () => void
85
96
  }
86
97
 
87
98
  /**
@@ -98,6 +109,7 @@ function Root({
98
109
  dismissable = true,
99
110
  closeOnEscape = true,
100
111
  closeAnimationDuration = 'fast',
112
+ onExitComplete,
101
113
  }: RootProps) {
102
114
  const {present, closing, requestOpen, requestClose} = useOverlayOpenState({
103
115
  open: controlledOpen,
@@ -113,10 +125,18 @@ function Root({
113
125
  const panelRef = useRef<View>(null)
114
126
  const registerClose = useCallback(() => () => {}, [])
115
127
 
116
- // Back to the trigger once the dialog is gone.
128
+ const onExitCompleteRef = useRef(onExitComplete)
129
+ useEffect(() => {
130
+ onExitCompleteRef.current = onExitComplete
131
+ })
132
+
133
+ // Back to the trigger once the dialog is gone, then tell the owner.
117
134
  const wasPresent = useRef(present)
118
135
  useEffect(() => {
119
- if (wasPresent.current && !present) focusForAccessibility(triggerRef.current, 'Dialog')
136
+ if (wasPresent.current && !present) {
137
+ focusForAccessibility(triggerRef.current, 'Dialog')
138
+ onExitCompleteRef.current?.()
139
+ }
120
140
  wasPresent.current = present
121
141
  }, [present])
122
142
 
@@ -680,3 +680,124 @@ export const EscapeClosesPopoverFirst: Story = {
680
680
  await waitFor(() => expect(page.queryByRole('dialog', {name: 'Host'})).not.toBeInTheDocument())
681
681
  },
682
682
  }
683
+
684
+ // --- onExitComplete ---
685
+
686
+ interface ExitCompleteOwnerProps {
687
+ outside: boolean
688
+ }
689
+
690
+ function ExitCompleteOwner({outside}: ExitCompleteOwnerProps) {
691
+ const [mounted, setMounted] = useState(true)
692
+ const [open, setOpen] = useState(false)
693
+ const [exits, setExits] = useState(0)
694
+ const [focusOnOpener, setFocusOnOpener] = useState('not yet')
695
+ const [order, setOrder] = useState<string[]>([])
696
+ const openerRef = useRef<HTMLButtonElement>(null)
697
+ return (
698
+ <Stack gap="sm">
699
+ <html.button ref={openerRef} onClick={() => setOpen(true)}>
700
+ Open notice
701
+ </html.button>
702
+ {outside && <Button onClick={() => setOpen(false)}>Close from outside</Button>}
703
+ <Text>{`Mounted: ${mounted ? 'yes' : 'no'}`}</Text>
704
+ <Text>{`Exits reported: ${exits}`}</Text>
705
+ <Text>{`Focus on opener at notice: ${focusOnOpener}`}</Text>
706
+ <Text>{`Order: ${order.join(', ')}`}</Text>
707
+ {mounted && (
708
+ <Dialog.Root
709
+ open={open}
710
+ onOpenChange={(next) => {
711
+ setOpen(next)
712
+ if (!next) setOrder((log) => [...log, 'onOpenChange(false)'])
713
+ }}
714
+ onExitComplete={() => {
715
+ setFocusOnOpener(document.activeElement === openerRef.current ? 'yes' : 'no')
716
+ setExits((count) => count + 1)
717
+ setOrder((log) => [...log, 'onExitComplete'])
718
+ setMounted(false)
719
+ }}
720
+ >
721
+ <Dialog.Portal size="sm" aria-label="Notice">
722
+ <Dialog.Body>
723
+ <Text>Notice body</Text>
724
+ </Dialog.Body>
725
+ </Dialog.Portal>
726
+ </Dialog.Root>
727
+ )}
728
+ </Stack>
729
+ )
730
+ }
731
+
732
+ /** An owner that unmounts on the notice after an outside close still gets focus back. */
733
+ export const UnmountOnExitCompleteOutside: Story = {
734
+ render: () => <ExitCompleteOwner outside />,
735
+ play: async ({canvasElement}) => {
736
+ const canvas = within(canvasElement)
737
+ const page = within(canvasElement.ownerDocument.body)
738
+ await userEvent.click(canvas.getByRole('button', {name: 'Open notice'}))
739
+ await page.findByRole('dialog')
740
+ await userEvent.click(page.getByRole('button', {name: 'Close from outside'}))
741
+ await waitFor(() => expect(canvas.getByText('Mounted: no')).toBeInTheDocument())
742
+ await expect(canvas.getByText('Exits reported: 1')).toBeInTheDocument()
743
+ await expect(canvas.getByText('Focus on opener at notice: yes')).toBeInTheDocument()
744
+ },
745
+ }
746
+
747
+ /** Escape reports `onOpenChange(false)` first, then the notice, and focus is on the opener. */
748
+ export const UnmountOnExitCompleteEscape: Story = {
749
+ render: () => <ExitCompleteOwner outside={false} />,
750
+ play: async ({canvasElement}) => {
751
+ const canvas = within(canvasElement)
752
+ const page = within(canvasElement.ownerDocument.body)
753
+ await userEvent.click(canvas.getByRole('button', {name: 'Open notice'}))
754
+ await page.findByRole('dialog')
755
+ await userEvent.keyboard('{Escape}')
756
+ await waitFor(() => expect(canvas.getByText('Mounted: no')).toBeInTheDocument())
757
+ await expect(canvas.getByText('Exits reported: 1')).toBeInTheDocument()
758
+ await expect(canvas.getByText('Focus on opener at notice: yes')).toBeInTheDocument()
759
+ await expect(canvas.getByText('Order: onOpenChange(false), onExitComplete')).toBeInTheDocument()
760
+ },
761
+ }
762
+
763
+ function ReopenMidExitDemo() {
764
+ const [open, setOpen] = useState(true)
765
+ const [exits, setExits] = useState(0)
766
+ return (
767
+ <Stack gap="sm">
768
+ <Button onClick={() => setOpen(false)}>Close it</Button>
769
+ <Button onClick={() => setOpen(true)}>Reopen it</Button>
770
+ <Text>{`Exits reported: ${exits}`}</Text>
771
+ <Dialog.Root
772
+ open={open}
773
+ onOpenChange={setOpen}
774
+ closeAnimationDuration="slow"
775
+ onExitComplete={() => setExits((count) => count + 1)}
776
+ >
777
+ <Dialog.Portal size="sm" aria-label="Reopened">
778
+ <Dialog.Body>
779
+ <Text>Body</Text>
780
+ </Dialog.Body>
781
+ </Dialog.Portal>
782
+ </Dialog.Root>
783
+ </Stack>
784
+ )
785
+ }
786
+
787
+ /** A reopen during the exit cancels it: no notice, then one after a full close. */
788
+ export const ReopenMidExitReportsNothing: Story = {
789
+ render: () => <ReopenMidExitDemo />,
790
+ play: async ({canvasElement}) => {
791
+ const canvas = within(canvasElement)
792
+ const page = within(canvasElement.ownerDocument.body)
793
+ await page.findByRole('dialog')
794
+ await userEvent.click(canvas.getByRole('button', {name: 'Close it'}))
795
+ await userEvent.click(canvas.getByRole('button', {name: 'Reopen it'}))
796
+ // Past the slow exit timer the reopen cancelled.
797
+ await new Promise((resolve) => setTimeout(resolve, DURATION_MS.slow * 2))
798
+ await expect(canvas.getByText('Exits reported: 0')).toBeInTheDocument()
799
+ await expect(page.getByRole('dialog')).toBeInTheDocument()
800
+ await userEvent.click(canvas.getByRole('button', {name: 'Close it'}))
801
+ await waitFor(() => expect(canvas.getByText('Exits reported: 1')).toBeInTheDocument())
802
+ },
803
+ }
@@ -60,6 +60,15 @@ interface RootProps {
60
60
  initialFocus?: RefObject<HTMLElement | null>
61
61
  /** Motion token for the close animation. Default: 'fast' (150ms) */
62
62
  closeAnimationDuration?: DurationToken
63
+ /**
64
+ * Called once per finished exit, after focus is back on what held it before
65
+ * the open: the place to unmount. For an inside close (Escape, backdrop,
66
+ * Dialog.Close) `onOpenChange(false)` runs first, then `onExitComplete`; an
67
+ * outside close (a controlled `open` turning false) calls only
68
+ * `onExitComplete`. Not called for an exit cancelled by a reopen. An owner
69
+ * that unmounts on `onOpenChange(false)` instead loses the focus hand-back.
70
+ */
71
+ onExitComplete?: () => void
63
72
  }
64
73
 
65
74
  function Root({
@@ -71,6 +80,7 @@ function Root({
71
80
  closeOnEscape = true,
72
81
  initialFocus,
73
82
  closeAnimationDuration = 'fast',
83
+ onExitComplete,
74
84
  }: RootProps) {
75
85
  const {
76
86
  open: isOpen,
@@ -116,6 +126,10 @@ function Root({
116
126
  closeCountRef.current -= 1
117
127
  }
118
128
  }, [])
129
+ const onExitCompleteRef = useRef(onExitComplete)
130
+ useEffect(() => {
131
+ onExitCompleteRef.current = onExitComplete
132
+ })
119
133
  const previousFocusRef = useRef<HTMLElement | null>(null)
120
134
  const wasOpenRef = useRef(false)
121
135
  useEffect(() => {
@@ -137,6 +151,7 @@ function Root({
137
151
  const target = previousFocusRef.current
138
152
  previousFocusRef.current = null
139
153
  if (target?.isConnected) target.focus({preventScroll: true})
154
+ onExitCompleteRef.current?.()
140
155
  }
141
156
  wasOpenRef.current = present
142
157
  }, [isOpen, present, initialFocus, closeOnEscape])