@re-venue/ui-core-react 0.3.1 → 0.3.3

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": "@re-venue/ui-core-react",
3
- "version": "0.3.1",
3
+ "version": "0.3.3",
4
4
  "type": "module",
5
5
  "main": "./dist/ui-core-react.umd.cjs",
6
6
  "module": "./dist/ui-core-react.js",
@@ -1,5 +1,5 @@
1
1
  import { useMemo, useRef, useState, useEffect } from 'react'
2
- import type { FocusEvent, ReactNode } from 'react'
2
+ import type { FocusEvent, MouseEvent, ReactNode } from 'react'
3
3
  import { ConfirmModal } from './ConfirmModal'
4
4
  import { useTheme } from '../hooks/useTheme'
5
5
  import type { AuthUser, SidebarNavItem, Venue } from '../types'
@@ -27,6 +27,7 @@ interface Props {
27
27
  * reload (rather than reactively) should leave this off.
28
28
  */
29
29
  inlineVenueSwitch?: boolean
30
+ onBeforeNavigate?: (proceed: () => void) => void
30
31
  children: ReactNode
31
32
  }
32
33
 
@@ -81,6 +82,7 @@ export function AppShell({
81
82
  sidebarPanel,
82
83
  scrollable = true,
83
84
  inlineVenueSwitch,
85
+ onBeforeNavigate,
84
86
  children,
85
87
  }: Props) {
86
88
  const { theme, toggleTheme } = useTheme()
@@ -226,14 +228,44 @@ export function AppShell({
226
228
  })
227
229
  }
228
230
 
231
+ function guardNavigation(navigate: () => void) {
232
+ if (onBeforeNavigate) {
233
+ onBeforeNavigate(navigate)
234
+ return
235
+ }
236
+ navigate()
237
+ }
238
+
239
+ function onLinkClick(event: MouseEvent<HTMLAnchorElement>, url: string) {
240
+ if (!onBeforeNavigate) {
241
+ return
242
+ }
243
+ const isModified = event.button !== 0 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey
244
+ if (isModified || event.currentTarget.target) {
245
+ return
246
+ }
247
+ event.preventDefault()
248
+ onBeforeNavigate(() => window.location.assign(url))
249
+ }
250
+
251
+ function onGuardedLogoutConfirm() {
252
+ setShowLogoutConfirm(false)
253
+ guardNavigation(onLogout)
254
+ }
255
+
229
256
  function onWorkspaceTitleClick(workspaceId: string) {
230
- onSwitchWorkspace?.(workspaceId)
231
257
  const url = workspaceNavItem?.url
232
- if (url) {
258
+ guardNavigation(() => {
259
+ onSwitchWorkspace?.(workspaceId)
260
+ if (!url && expanded.has(workspaceId)) {
261
+ return
262
+ }
263
+ if (!url) {
264
+ toggleWorkspace(workspaceId)
265
+ return
266
+ }
233
267
  window.location.assign(url)
234
- } else if (!expanded.has(workspaceId)) {
235
- toggleWorkspace(workspaceId)
236
- }
268
+ })
237
269
  }
238
270
 
239
271
  function onVenueClick(venueId: string) {
@@ -245,11 +277,13 @@ export function AppShell({
245
277
  const url = stayHere ? activeNavItem!.url : firstHeaderItem?.url
246
278
  const alreadyOnDestination = url ? currentHref.startsWith(url) : false
247
279
  if (venueId === activeVenueId && alreadyOnDestination) return
248
- if (venueId !== activeVenueId) onSwitchVenue?.(venueId)
249
- // If the consumer applies the venue switch inline and we'd only be reloading the
250
- // page we're already on, skip the disruptive full-page reload.
251
- if (inlineVenueSwitch && alreadyOnDestination) return
252
- if (url) window.location.assign(url)
280
+ guardNavigation(() => {
281
+ if (venueId !== activeVenueId) onSwitchVenue?.(venueId)
282
+ // If the consumer applies the venue switch inline and we'd only be reloading the
283
+ // page we're already on, skip the disruptive full-page reload.
284
+ if (inlineVenueSwitch && alreadyOnDestination) return
285
+ if (url) window.location.assign(url)
286
+ })
253
287
  }
254
288
 
255
289
  const showHeader = headerNavItems.length > 0 || settingsNavItems.length > 0 || headerActions !== undefined || headerSlot !== undefined || (collapsed && !!activeVenue)
@@ -321,6 +355,7 @@ export function AppShell({
321
355
  <a
322
356
  key={item.url}
323
357
  href={item.url}
358
+ onClick={(event) => onLinkClick(event, item.url)}
324
359
  className={`rv-nav-item${item.url === activeNavUrl ? ' rv-active' : ''}`}
325
360
  title={railMode ? item.label : undefined}
326
361
  >
@@ -415,7 +450,12 @@ export function AppShell({
415
450
  </button>
416
451
  )}
417
452
  {accountSettingsUrl && !railMode && (
418
- <a className="rv-account-settings-btn" href={accountSettingsUrl} title="Account settings">
453
+ <a
454
+ className="rv-account-settings-btn"
455
+ href={accountSettingsUrl}
456
+ onClick={(event) => onLinkClick(event, accountSettingsUrl)}
457
+ title="Account settings"
458
+ >
419
459
  <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
420
460
  <path d="M12.22 2h-.44a2 2 0 0 0-2 2v.18a2 2 0 0 1-1 1.73l-.43.25a2 2 0 0 1-2 0l-.15-.08a2 2 0 0 0-2.73.73l-.22.38a2 2 0 0 0 .73 2.73l.15.1a2 2 0 0 1 1 1.72v.51a2 2 0 0 1-1 1.74l-.15.09a2 2 0 0 0-.73 2.73l.22.38a2 2 0 0 0 2.73.73l.15-.08a2 2 0 0 1 2 0l.43.25a2 2 0 0 1 1 1.73V20a2 2 0 0 0 2 2h.44a2 2 0 0 0 2-2v-.18a2 2 0 0 1 1-1.73l.43-.25a2 2 0 0 1 2 0l.15.08a2 2 0 0 0 2.73-.73l.22-.39a2 2 0 0 0-.73-2.73l-.15-.08a2 2 0 0 1-1-1.74v-.5a2 2 0 0 1 1-1.74l.15-.09a2 2 0 0 0 .73-2.73l-.22-.38a2 2 0 0 0-2.73-.73l-.15.08a2 2 0 0 1-2 0l-.43-.25a2 2 0 0 1-1-1.73V4a2 2 0 0 0-2-2z" />
421
461
  <circle cx="12" cy="12" r="3" />
@@ -452,6 +492,7 @@ export function AppShell({
452
492
  <a
453
493
  key={item.url}
454
494
  href={item.url}
495
+ onClick={(event) => onLinkClick(event, item.url)}
455
496
  className={`rv-header-nav-item${item.url === activeNavUrl ? ' rv-active' : ''}`}
456
497
  >
457
498
  <span className="rv-nav-icon" dangerouslySetInnerHTML={{ __html: item.icon }} />
@@ -470,6 +511,7 @@ export function AppShell({
470
511
  <a
471
512
  key={item.url}
472
513
  href={item.url}
514
+ onClick={(event) => onLinkClick(event, item.url)}
473
515
  className={`rv-header-action${item.url === activeNavUrl ? ' rv-active' : ''}`}
474
516
  >
475
517
  <span className="rv-header-action-icon" dangerouslySetInnerHTML={{ __html: item.icon }} />
@@ -492,7 +534,7 @@ export function AppShell({
492
534
  title="Sign out"
493
535
  message="Are you sure you want to sign out?"
494
536
  confirmLabel="Sign out"
495
- onConfirm={onLogout}
537
+ onConfirm={onBeforeNavigate ? onGuardedLogoutConfirm : onLogout}
496
538
  onCancel={() => setShowLogoutConfirm(false)}
497
539
  />
498
540
  </div>