@cratis/scene.react 4.27.0 → 4.29.0
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/core/coreDescriptors.ts +28 -1
- package/dist/cjs/core/coreDescriptors.js +64 -0
- package/dist/cjs/core/coreDescriptors.js.map +1 -1
- package/dist/cjs/index.js +16 -0
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/navigation/SceneNavigationHost.js +24 -13
- package/dist/cjs/navigation/SceneNavigationHost.js.map +1 -1
- package/dist/cjs/navigation/region/NavigationRegionClassNames.js +21 -0
- package/dist/cjs/navigation/region/NavigationRegionClassNames.js.map +1 -0
- package/dist/cjs/navigation/region/NavigationRegionView.js +283 -0
- package/dist/cjs/navigation/region/NavigationRegionView.js.map +1 -0
- package/dist/cjs/navigation/region/SceneNavigationModelContext.js +22 -0
- package/dist/cjs/navigation/region/SceneNavigationModelContext.js.map +1 -0
- package/dist/cjs/navigation/region/currentModuleOf.js +15 -0
- package/dist/cjs/navigation/region/currentModuleOf.js.map +1 -0
- package/dist/cjs/navigation/region/navigationDestinations.js +46 -0
- package/dist/cjs/navigation/region/navigationDestinations.js.map +1 -0
- package/dist/cjs/navigation/region/navigationItemActive.js +31 -0
- package/dist/cjs/navigation/region/navigationItemActive.js.map +1 -0
- package/dist/cjs/navigation/region/navigationItemDestination.js +34 -0
- package/dist/cjs/navigation/region/navigationItemDestination.js.map +1 -0
- package/dist/esm/core/coreDescriptors.d.ts.map +1 -1
- package/dist/esm/core/coreDescriptors.js +65 -1
- package/dist/esm/core/coreDescriptors.js.map +1 -1
- package/dist/esm/index.js +7 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/navigation/SceneNavigationHost.d.ts +5 -1
- package/dist/esm/navigation/SceneNavigationHost.d.ts.map +1 -1
- package/dist/esm/navigation/SceneNavigationHost.js +25 -14
- package/dist/esm/navigation/SceneNavigationHost.js.map +1 -1
- package/dist/esm/navigation/index.d.ts +8 -0
- package/dist/esm/navigation/index.d.ts.map +1 -1
- package/dist/esm/navigation/index.js +8 -0
- package/dist/esm/navigation/index.js.map +1 -1
- package/dist/esm/navigation/region/NavigationRegionClassNames.d.ts +12 -0
- package/dist/esm/navigation/region/NavigationRegionClassNames.d.ts.map +1 -0
- package/dist/esm/navigation/region/NavigationRegionClassNames.js +19 -0
- package/dist/esm/navigation/region/NavigationRegionClassNames.js.map +1 -0
- package/dist/esm/navigation/region/NavigationRegionView.d.ts +20 -0
- package/dist/esm/navigation/region/NavigationRegionView.d.ts.map +1 -0
- package/dist/esm/navigation/region/NavigationRegionView.js +281 -0
- package/dist/esm/navigation/region/NavigationRegionView.js.map +1 -0
- package/dist/esm/navigation/region/SceneNavigationModel.d.ts +9 -0
- package/dist/esm/navigation/region/SceneNavigationModel.d.ts.map +1 -0
- package/dist/esm/navigation/region/SceneNavigationModel.js +2 -0
- package/dist/esm/navigation/region/SceneNavigationModel.js.map +1 -0
- package/dist/esm/navigation/region/SceneNavigationModelContext.d.ts +9 -0
- package/dist/esm/navigation/region/SceneNavigationModelContext.d.ts.map +1 -0
- package/dist/esm/navigation/region/SceneNavigationModelContext.js +19 -0
- package/dist/esm/navigation/region/SceneNavigationModelContext.js.map +1 -0
- package/dist/esm/navigation/region/currentModuleOf.d.ts +3 -0
- package/dist/esm/navigation/region/currentModuleOf.d.ts.map +1 -0
- package/dist/esm/navigation/region/currentModuleOf.js +13 -0
- package/dist/esm/navigation/region/currentModuleOf.js.map +1 -0
- package/dist/esm/navigation/region/navigationDestinations.d.ts +4 -0
- package/dist/esm/navigation/region/navigationDestinations.d.ts.map +1 -0
- package/dist/esm/navigation/region/navigationDestinations.js +44 -0
- package/dist/esm/navigation/region/navigationDestinations.js.map +1 -0
- package/dist/esm/navigation/region/navigationItemActive.d.ts +5 -0
- package/dist/esm/navigation/region/navigationItemActive.d.ts.map +1 -0
- package/dist/esm/navigation/region/navigationItemActive.js +29 -0
- package/dist/esm/navigation/region/navigationItemActive.js.map +1 -0
- package/dist/esm/navigation/region/navigationItemDestination.d.ts +5 -0
- package/dist/esm/navigation/region/navigationItemDestination.d.ts.map +1 -0
- package/dist/esm/navigation/region/navigationItemDestination.js +31 -0
- package/dist/esm/navigation/region/navigationItemDestination.js.map +1 -0
- package/dist/esm/tsconfig.tsbuildinfo +1 -1
- package/navigation/SceneNavigationHost.tsx +27 -6
- package/navigation/index.ts +8 -0
- package/navigation/region/NavigationRegionClassNames.ts +46 -0
- package/navigation/region/NavigationRegionView.tsx +234 -0
- package/navigation/region/SceneNavigationModel.ts +29 -0
- package/navigation/region/SceneNavigationModelContext.tsx +25 -0
- package/navigation/region/currentModuleOf.ts +14 -0
- package/navigation/region/navigationDestinations.ts +41 -0
- package/navigation/region/navigationItemActive.ts +39 -0
- package/navigation/region/navigationItemDestination.ts +29 -0
- package/package.json +5 -5
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// Copyright (c) Cratis. All rights reserved.
|
|
2
2
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
3
|
|
|
4
|
-
import { useMemo, useState } from 'react';
|
|
4
|
+
import { ReactNode, useMemo, useState } from 'react';
|
|
5
5
|
import { BindingScope } from '@cratis/scene.engine';
|
|
6
6
|
import { DestinationReference, SceneElement } from '@cratis/scene.model';
|
|
7
7
|
import { SceneElementView } from '../SceneElementView';
|
|
@@ -14,6 +14,9 @@ import { sceneRoutesFrom } from './sceneRoutesFrom';
|
|
|
14
14
|
import { collectOutletOwners } from './collectOutletOwners';
|
|
15
15
|
import { SceneOutletContext } from './SceneOutletContext';
|
|
16
16
|
import { useSceneHistoryNavigation } from './useSceneHistoryNavigation';
|
|
17
|
+
import { SceneNavigationModel } from './region/SceneNavigationModel';
|
|
18
|
+
import { SceneNavigationModelProvider } from './region/SceneNavigationModelContext';
|
|
19
|
+
import { navigationDestinations } from './region/navigationDestinations';
|
|
17
20
|
|
|
18
21
|
export interface SceneNavigationHostProps {
|
|
19
22
|
screens: Record<string, SceneElement>;
|
|
@@ -31,6 +34,18 @@ export interface SceneNavigationHostProps {
|
|
|
31
34
|
/** The destinations the application navigates to, so their route overrides can be deep linked. */
|
|
32
35
|
destinations?: DestinationReference[];
|
|
33
36
|
|
|
37
|
+
/**
|
|
38
|
+
* The application's structure and navigation. Navigation regions rendered inside the host (`NavigationRegionView`) read it,
|
|
39
|
+
* and - when `destinations` is not given - the destinations of every navigation item become the deep-linkable ones.
|
|
40
|
+
*/
|
|
41
|
+
navigationModel?: SceneNavigationModel;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Wraps the primary outlet in the application's chrome - a topbar, a sidebar, a toolbar. The chrome is part of the host,
|
|
45
|
+
* so it stays mounted while the screens inside it change and navigation regions keep their state.
|
|
46
|
+
*/
|
|
47
|
+
frame?(content: ReactNode): ReactNode;
|
|
48
|
+
|
|
34
49
|
/** An explicit route table, used instead of the one derived from `screens` and `destinations`. */
|
|
35
50
|
routes?: SceneRoute[];
|
|
36
51
|
}
|
|
@@ -42,7 +57,10 @@ export interface SceneNavigationHostProps {
|
|
|
42
57
|
* dialog destinations render dialog content over the screen that opened them, and external destinations
|
|
43
58
|
* only report their resolved action. Every navigation is a history entry.
|
|
44
59
|
*/
|
|
45
|
-
export function SceneNavigationHost({ screens, dialogs = {}, initialScreen, registry, bindingScope = {}, history, destinations, routes }: SceneNavigationHostProps) {
|
|
60
|
+
export function SceneNavigationHost({ screens, dialogs = {}, initialScreen, registry, bindingScope = {}, history, destinations: authoredDestinations, routes, navigationModel, frame }: SceneNavigationHostProps) {
|
|
61
|
+
const destinations = useMemo(
|
|
62
|
+
() => authoredDestinations ?? (navigationModel ? navigationDestinations(navigationModel.structure, navigationModel.navigation, navigationModel.screenFor) : undefined),
|
|
63
|
+
[authoredDestinations, navigationModel]);
|
|
46
64
|
const [memoryHistory] = useState(() => createMemorySceneHistory());
|
|
47
65
|
const routeTable = useMemo(() => routes ?? sceneRoutesFrom(Object.keys(screens), destinations), [destinations, routes, screens]);
|
|
48
66
|
const outletOwners = useMemo(() => collectOutletOwners(screens), [screens]);
|
|
@@ -52,12 +70,15 @@ export function SceneNavigationHost({ screens, dialogs = {}, initialScreen, regi
|
|
|
52
70
|
const screen = screens[value.primaryScreen];
|
|
53
71
|
const dialog = value.currentDialog ? dialogs[value.currentDialog] : undefined;
|
|
54
72
|
|
|
55
|
-
|
|
56
|
-
<
|
|
57
|
-
|
|
58
|
-
|
|
73
|
+
const outlet = <div data-scene-outlet={value.primaryScreen === value.currentScreen ? value.currentOutlet ?? 'primary' : 'primary'} data-scene-url={value.currentUrl}>
|
|
74
|
+
{screen && <SceneElementView element={screen} registry={registry} dataContext={bindingScope.dataContext} queryResults={bindingScope.queryResults} componentOutputs={bindingScope.componentOutputs} />}
|
|
75
|
+
</div>;
|
|
76
|
+
const page = <SceneNavigationProvider value={value}><SceneOutletContext.Provider value={outletContext}>
|
|
77
|
+
{frame ? frame(outlet) : outlet}
|
|
59
78
|
{dialog && <div role='dialog' aria-modal='true' aria-label={value.currentDialog} data-scene-dialog={value.currentDialog}>
|
|
60
79
|
<SceneElementView element={dialog} registry={registry} dataContext={bindingScope.dataContext} queryResults={bindingScope.queryResults} componentOutputs={bindingScope.componentOutputs} />
|
|
61
80
|
</div>}
|
|
62
81
|
</SceneOutletContext.Provider></SceneNavigationProvider>;
|
|
82
|
+
|
|
83
|
+
return navigationModel ? <SceneNavigationModelProvider model={navigationModel}>{page}</SceneNavigationModelProvider> : page;
|
|
63
84
|
}
|
package/navigation/index.ts
CHANGED
|
@@ -22,3 +22,11 @@ export * from './CoreOutlet';
|
|
|
22
22
|
export * from './SceneOutletContext';
|
|
23
23
|
export * from './collectOutletOwners';
|
|
24
24
|
export * from './placeInOutlet';
|
|
25
|
+
export * from './region/SceneNavigationModel';
|
|
26
|
+
export * from './region/SceneNavigationModelContext';
|
|
27
|
+
export * from './region/NavigationRegionClassNames';
|
|
28
|
+
export * from './region/NavigationRegionView';
|
|
29
|
+
export * from './region/currentModuleOf';
|
|
30
|
+
export * from './region/navigationItemActive';
|
|
31
|
+
export * from './region/navigationItemDestination';
|
|
32
|
+
export * from './region/navigationDestinations';
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The class names a {@link NavigationRegionView} puts on its nodes. The renderer carries no look of its own: a package
|
|
7
|
+
* gives the nodes its vocabulary (`blueprint.default` uses its `layout-menu` classes) and a stylesheet does the rest.
|
|
8
|
+
* State is always on data attributes as well - `data-active`, `data-expanded`, `data-read-only`, `data-origin`.
|
|
9
|
+
*/
|
|
10
|
+
export interface NavigationRegionClassNames {
|
|
11
|
+
/** The `nav` landmark. */
|
|
12
|
+
root: string;
|
|
13
|
+
|
|
14
|
+
/** Each list of items. */
|
|
15
|
+
list: string;
|
|
16
|
+
|
|
17
|
+
/** Each item's `li`. */
|
|
18
|
+
item: string;
|
|
19
|
+
|
|
20
|
+
/** The link of an item. */
|
|
21
|
+
link: string;
|
|
22
|
+
|
|
23
|
+
/** The icon of an item. */
|
|
24
|
+
icon: string;
|
|
25
|
+
|
|
26
|
+
/** The title of an item. */
|
|
27
|
+
label: string;
|
|
28
|
+
|
|
29
|
+
/** The button that expands or collapses an item's sub-items. */
|
|
30
|
+
toggle: string;
|
|
31
|
+
|
|
32
|
+
/** The list of an item's sub-items. */
|
|
33
|
+
submenu: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The class names used when a host passes none. */
|
|
37
|
+
export const defaultNavigationRegionClassNames: NavigationRegionClassNames = {
|
|
38
|
+
root: 'scene-nav',
|
|
39
|
+
list: 'scene-nav-list',
|
|
40
|
+
item: 'scene-nav-item',
|
|
41
|
+
link: 'scene-nav-link',
|
|
42
|
+
icon: 'scene-nav-icon',
|
|
43
|
+
label: 'scene-nav-label',
|
|
44
|
+
toggle: 'scene-nav-toggle',
|
|
45
|
+
submenu: 'scene-nav-submenu',
|
|
46
|
+
};
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { KeyboardEvent, MouseEvent, ReactNode, useContext, useMemo, useRef, useState } from 'react';
|
|
5
|
+
import { DestinationReference, NavigationRegionKind, parseIconReference } from '@cratis/scene.model';
|
|
6
|
+
import { ApplicationStructureIndex, EffectiveNavItem, EffectiveNavigationRegion, effectiveNavigation } from '@cratis/scene.engine';
|
|
7
|
+
import { SceneIcon } from '../../icons/SceneIcon';
|
|
8
|
+
import { useIconAdapters } from '../../icons/IconAdapterContext';
|
|
9
|
+
import { resolveDestination } from '../resolveDestination';
|
|
10
|
+
import { SceneNavigationContextInternal } from '../SceneNavigationContext';
|
|
11
|
+
import { NavigationRegionClassNames, defaultNavigationRegionClassNames } from './NavigationRegionClassNames';
|
|
12
|
+
import { SceneNavigationModel } from './SceneNavigationModel';
|
|
13
|
+
import { useSceneNavigationModel } from './SceneNavigationModelContext';
|
|
14
|
+
import { currentModuleOf } from './currentModuleOf';
|
|
15
|
+
import { navigationItemActive } from './navigationItemActive';
|
|
16
|
+
import { defaultScreenFor, navigationItemDestination } from './navigationItemDestination';
|
|
17
|
+
|
|
18
|
+
export interface NavigationRegionViewProps {
|
|
19
|
+
/** Which region to show. */
|
|
20
|
+
region: NavigationRegionKind;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The module to show the region of. When absent the region follows the module of the screen on the page, so
|
|
24
|
+
* navigating to another module recomputes the effective items in place; outside every module it is the application's.
|
|
25
|
+
*/
|
|
26
|
+
module?: string;
|
|
27
|
+
|
|
28
|
+
/** Pins the region to the application level, whatever screen is on the page. */
|
|
29
|
+
application?: boolean;
|
|
30
|
+
|
|
31
|
+
/** What to resolve against; the {@link SceneNavigationModelProvider}'s when absent. */
|
|
32
|
+
model?: SceneNavigationModel;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Which sub-items of a sidebar show: only those of the parents of the active item and of items the user expanded
|
|
36
|
+
* (`active`, the default), or all of them (`all`) - a design layer wants the whole hierarchy.
|
|
37
|
+
*/
|
|
38
|
+
expand?: 'active' | 'all';
|
|
39
|
+
|
|
40
|
+
/** The accessible name of the `nav` landmark. */
|
|
41
|
+
label?: string;
|
|
42
|
+
|
|
43
|
+
/** The class names to put on the nodes. */
|
|
44
|
+
classNames?: Partial<NavigationRegionClassNames>;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Draws an item's icon from its qualified reference. By default the icon is drawn through the adapters of the
|
|
48
|
+
* enclosing `IconAdapterProvider`, or not at all without one.
|
|
49
|
+
*/
|
|
50
|
+
renderIcon?(icon: string): ReactNode;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Content a host adds to an item after its title - Studio design mode draws the lock of an inherited item here; Stage
|
|
54
|
+
* ignores it. The item carries `origin` and `readOnly`.
|
|
55
|
+
*/
|
|
56
|
+
renderAdornment?(item: EffectiveNavItem): ReactNode;
|
|
57
|
+
|
|
58
|
+
/** The href of an item's link, for opening it in a new tab; the hash route of the destination by default. */
|
|
59
|
+
hrefFor?(destination: DestinationReference, url: string | undefined): string | undefined;
|
|
60
|
+
|
|
61
|
+
/** Called after an item has been activated and the host has navigated. */
|
|
62
|
+
onActivate?(item: EffectiveNavItem, destination: DestinationReference): void;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function itemKey(path: string[]): string {
|
|
66
|
+
return path.join('/');
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function containsActive(item: EffectiveNavItem, isActive: (item: EffectiveNavItem) => boolean): boolean {
|
|
70
|
+
return isActive(item) || (item.children ?? []).some(child => containsActive(child, isActive));
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Renders the effective navigation of one region - a topbar or a sidebar - for the application or a module.
|
|
75
|
+
*
|
|
76
|
+
* - **Items** come from `effectiveNavigation`, so inherit/extend/override is already applied; nothing here re-implements
|
|
77
|
+
* it. A topbar is flat; a sidebar nests, collapses, and expands the parents of the active item.
|
|
78
|
+
* - **Active state** follows what the navigation host shows: an item is active (`aria-current`, `data-active`) when the
|
|
79
|
+
* screen it opens is the one in its slot, so items rendering into different slots are active at once.
|
|
80
|
+
* - **Activation** goes through the enclosing navigation host (`navigate`), which renders the target in the item's slot -
|
|
81
|
+
* the `outlet` of the destination; outside a host it announces `cratis.scene.navigate`.
|
|
82
|
+
* - **Authorization**: `isVisible` of the model hides items, it does not disable them.
|
|
83
|
+
* - **Keyboard**: arrows (left/right in a topbar, up/down in a sidebar), Home and End move between items; in a sidebar
|
|
84
|
+
* right expands and left collapses (or goes to the parent).
|
|
85
|
+
*
|
|
86
|
+
* Nothing here draws design chrome: items carry `data-read-only` and `data-origin` for a design layer to style.
|
|
87
|
+
*/
|
|
88
|
+
export function NavigationRegionView({ region, module, application = false, model: modelProp, label, expand = 'active', classNames, renderIcon, renderAdornment, hrefFor, onActivate }: NavigationRegionViewProps) {
|
|
89
|
+
const contextModel = useSceneNavigationModel();
|
|
90
|
+
const model = modelProp ?? contextModel;
|
|
91
|
+
if (!model) throw new Error('NavigationRegionView needs a navigation model: pass `model` or render it inside a SceneNavigationModelProvider.');
|
|
92
|
+
|
|
93
|
+
const navigation = useContext(SceneNavigationContextInternal);
|
|
94
|
+
const adapters = useIconAdapters();
|
|
95
|
+
const names = { ...defaultNavigationRegionClassNames, ...classNames };
|
|
96
|
+
const index = useMemo(() => model.structure instanceof ApplicationStructureIndex ? model.structure : new ApplicationStructureIndex(model.structure), [model.structure]);
|
|
97
|
+
const screenFor = model.screenFor ?? defaultScreenFor;
|
|
98
|
+
|
|
99
|
+
const sidebar = region === NavigationRegionKind.Sidebar;
|
|
100
|
+
const placement = useMemo(() => ({ primaryScreen: navigation?.primaryScreen, outlets: navigation?.outlets ?? {} }), [navigation?.outlets, navigation?.primaryScreen]);
|
|
101
|
+
const scope = application ? undefined : module ?? currentModuleOf(index, placement.primaryScreen);
|
|
102
|
+
const resolved: EffectiveNavigationRegion = useMemo(() => effectiveNavigation(index, model.navigation, scope, region), [index, model.navigation, scope, region]);
|
|
103
|
+
|
|
104
|
+
const isActive = (item: EffectiveNavItem) => placement.primaryScreen !== undefined && navigationItemActive(item, { primaryScreen: placement.primaryScreen, outlets: placement.outlets }, screenFor, !sidebar);
|
|
105
|
+
const isVisible = model.isVisible ?? (() => true);
|
|
106
|
+
const visible = useMemo(() => {
|
|
107
|
+
const keep = (items: EffectiveNavItem[]): EffectiveNavItem[] => items.filter(isVisible).map(item => item.children ? { ...item, children: keep(item.children) } : item);
|
|
108
|
+
return keep(resolved.items);
|
|
109
|
+
}, [resolved, model.isVisible]);
|
|
110
|
+
|
|
111
|
+
const activeKey = JSON.stringify([placement.primaryScreen, placement.outlets, scope]);
|
|
112
|
+
const [toggled, setToggled] = useState<{ key: string; state: Record<string, boolean> }>({ key: activeKey, state: {} });
|
|
113
|
+
const overrides = toggled.key === activeKey ? toggled.state : {};
|
|
114
|
+
const [focused, setFocused] = useState<string>();
|
|
115
|
+
const root = useRef<HTMLElement>(null);
|
|
116
|
+
|
|
117
|
+
const isExpanded = (item: EffectiveNavItem, key: string) => overrides[key] ?? (expand === 'all' || containsActive(item, isActive));
|
|
118
|
+
const toggle = (key: string, expanded: boolean) => setToggled({ key: activeKey, state: { ...overrides, [key]: expanded } });
|
|
119
|
+
|
|
120
|
+
const activate = (item: EffectiveNavItem) => {
|
|
121
|
+
const destination = navigationItemDestination(item, screenFor);
|
|
122
|
+
if (destination === undefined) return;
|
|
123
|
+
if (navigation) {
|
|
124
|
+
navigation.navigate(destination);
|
|
125
|
+
} else {
|
|
126
|
+
globalThis.dispatchEvent(new CustomEvent('cratis.scene.navigate', { detail: { targetScreen: destination.screen ?? '', destination, item } }));
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
onActivate?.(item, destination);
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
const links = () => Array.from(root.current?.querySelectorAll<HTMLElement>('[data-scene-nav-link]') ?? []);
|
|
133
|
+
const focusLink = (element: HTMLElement | undefined) => {
|
|
134
|
+
if (!element) return;
|
|
135
|
+
setFocused(element.getAttribute('data-scene-nav-link') ?? undefined);
|
|
136
|
+
element.focus();
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
const onKeyDown = (event: KeyboardEvent<HTMLElement>) => {
|
|
140
|
+
const all = links();
|
|
141
|
+
const position = all.findIndex(link => link === document.activeElement);
|
|
142
|
+
if (position < 0) return;
|
|
143
|
+
const next = sidebar ? 'ArrowDown' : 'ArrowRight';
|
|
144
|
+
const previous = sidebar ? 'ArrowUp' : 'ArrowLeft';
|
|
145
|
+
const current = all[position];
|
|
146
|
+
const key = current.getAttribute('data-scene-nav-link')!;
|
|
147
|
+
const owner = current.closest('[data-scene-nav-item]') as HTMLElement | null;
|
|
148
|
+
const hasChildren = owner?.getAttribute('data-scene-nav-parent') === 'true';
|
|
149
|
+
const expanded = owner?.getAttribute('data-expanded') === 'true';
|
|
150
|
+
|
|
151
|
+
let target: HTMLElement | undefined;
|
|
152
|
+
if (event.key === next) target = all[Math.min(position + 1, all.length - 1)];
|
|
153
|
+
else if (event.key === previous) target = all[Math.max(position - 1, 0)];
|
|
154
|
+
else if (event.key === 'Home') target = all[0];
|
|
155
|
+
else if (event.key === 'End') target = all[all.length - 1];
|
|
156
|
+
else if (sidebar && event.key === 'ArrowRight' && hasChildren) {
|
|
157
|
+
if (!expanded) toggle(key, true);
|
|
158
|
+
else target = all[position + 1];
|
|
159
|
+
} else if (sidebar && event.key === 'ArrowLeft') {
|
|
160
|
+
if (hasChildren && expanded) toggle(key, false);
|
|
161
|
+
else {
|
|
162
|
+
const parent = owner?.parentElement?.closest('[data-scene-nav-item]')?.querySelector<HTMLElement>(':scope > [data-scene-nav-link]');
|
|
163
|
+
if (parent) target = parent;
|
|
164
|
+
}
|
|
165
|
+
} else return;
|
|
166
|
+
|
|
167
|
+
event.preventDefault();
|
|
168
|
+
focusLink(target);
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
const tabStop = focused ?? undefined;
|
|
172
|
+
const firstActive = (items: EffectiveNavItem[], path: string[]): string | undefined => {
|
|
173
|
+
for (const item of items) {
|
|
174
|
+
const key = itemKey([...path, item.id]);
|
|
175
|
+
if (isActive(item)) return key;
|
|
176
|
+
const inner = firstActive(item.children ?? [], [...path, item.id]);
|
|
177
|
+
if (inner !== undefined && isExpanded(item, key)) return inner;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
return undefined;
|
|
181
|
+
};
|
|
182
|
+
const firstKey = visible.length > 0 ? itemKey([visible[0].id]) : undefined;
|
|
183
|
+
const stop = tabStop ?? firstActive(visible, []) ?? firstKey;
|
|
184
|
+
|
|
185
|
+
const icon = (item: EffectiveNavItem) => {
|
|
186
|
+
if (item.icon === undefined) return null;
|
|
187
|
+
const drawn = renderIcon ? renderIcon(item.icon) : adapters ? (() => {
|
|
188
|
+
const reference = parseIconReference(item.icon!);
|
|
189
|
+
return reference ? <SceneIcon reference={reference} adapters={adapters} /> : null;
|
|
190
|
+
})() : null;
|
|
191
|
+
return drawn === null ? null : <span className={names.icon} aria-hidden='true' data-scene-icon={item.icon}>{drawn}</span>;
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
const renderItems = (items: EffectiveNavItem[], path: string[], level: number): ReactNode => items.map(item => {
|
|
195
|
+
const key = itemKey([...path, item.id]);
|
|
196
|
+
const destination = navigationItemDestination(item, screenFor);
|
|
197
|
+
const active = isActive(item);
|
|
198
|
+
const children = item.children ?? [];
|
|
199
|
+
const parent = sidebar && children.length > 0;
|
|
200
|
+
const expanded = parent && isExpanded(item, key);
|
|
201
|
+
const url = destination ? resolveDestination(destination).url : undefined;
|
|
202
|
+
const href = destination ? (hrefFor ? hrefFor(destination, url) : url ? `#/${url}` : undefined) : undefined;
|
|
203
|
+
const onClick = (event: MouseEvent) => {
|
|
204
|
+
if (event.metaKey || event.ctrlKey || event.shiftKey || event.button !== 0) return;
|
|
205
|
+
event.preventDefault();
|
|
206
|
+
if (destination) activate(item);
|
|
207
|
+
else if (parent) toggle(key, !expanded);
|
|
208
|
+
};
|
|
209
|
+
const content = <>
|
|
210
|
+
{icon(item)}
|
|
211
|
+
<span className={names.label}>{item.title}</span>
|
|
212
|
+
{renderAdornment?.(item)}
|
|
213
|
+
</>;
|
|
214
|
+
|
|
215
|
+
return <li key={item.id} className={names.item} data-scene-nav-item={item.id} data-scene-nav-parent={parent ? 'true' : undefined}
|
|
216
|
+
data-active={active} data-active-descendant={!active && parent && children.some(child => containsActive(child, isActive)) ? true : undefined} data-expanded={parent ? expanded : undefined} data-origin={item.origin} data-read-only={item.readOnly}
|
|
217
|
+
data-slot={item.slot} data-level={level}>
|
|
218
|
+
{destination || parent
|
|
219
|
+
? <a className={names.link} href={href ?? '#'} data-scene-nav-link={key} aria-current={active ? 'page' : undefined}
|
|
220
|
+
aria-expanded={parent && !destination ? expanded : undefined} tabIndex={key === stop ? 0 : -1}
|
|
221
|
+
onClick={onClick} onFocus={() => setFocused(key)}>{content}</a>
|
|
222
|
+
: <span className={names.link} aria-disabled='true' data-scene-nav-link={key} tabIndex={-1}>{content}</span>}
|
|
223
|
+
{parent && <button type='button' className={names.toggle} tabIndex={-1} aria-expanded={expanded} aria-controls={`${key}-items`}
|
|
224
|
+
aria-label={`${expanded ? 'Collapse' : 'Expand'} ${item.title}`} onClick={() => toggle(key, !expanded)} />}
|
|
225
|
+
{parent && expanded && <ul id={`${key}-items`} className={`${names.list} ${names.submenu}`}>{renderItems(children, [...path, item.id], level + 1)}</ul>}
|
|
226
|
+
</li>;
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
return <nav ref={root} className={names.root} aria-label={label ?? (sidebar ? 'Sidebar' : 'Topbar')} data-scene-nav-region={region}
|
|
230
|
+
data-scene-nav-sources={resolved.sources.kind} data-scene-nav-sources-label={resolved.sources.label} data-scene-nav-module={scope}
|
|
231
|
+
data-scene-nav-mode={resolved.mode} onKeyDown={onKeyDown}>
|
|
232
|
+
<ul className={names.list}>{renderItems(visible, [], 0)}</ul>
|
|
233
|
+
</nav>;
|
|
234
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { ApplicationStructure, NavigationModel } from '@cratis/scene.model';
|
|
5
|
+
import { ApplicationStructureIndex, EffectiveNavItem } from '@cratis/scene.engine';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* What a navigation region needs to resolve the items it shows: the application's structure and its navigation, and
|
|
9
|
+
* the two hooks a host plugs.
|
|
10
|
+
*/
|
|
11
|
+
export interface SceneNavigationModel {
|
|
12
|
+
/** The modules, features and slices of the application. */
|
|
13
|
+
structure: ApplicationStructure | ApplicationStructureIndex;
|
|
14
|
+
|
|
15
|
+
/** The application and module navigation regions. */
|
|
16
|
+
navigation: NavigationModel;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The name of the screen an item opens. Defaults to the qualified id of the feature the item resolves to
|
|
20
|
+
* (`sales.orders`), which is how `ApplicationStructure` names a feature's screen.
|
|
21
|
+
*/
|
|
22
|
+
screenFor?(item: EffectiveNavItem): string | undefined;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Whether an item is shown. An item for which this answers false is hidden, not disabled - Stage plugs permissions
|
|
26
|
+
* here. Applies to every level of a hierarchical region; hiding a parent hides its children.
|
|
27
|
+
*/
|
|
28
|
+
isVisible?(item: EffectiveNavItem): boolean;
|
|
29
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { ReactNode, createContext, useContext } from 'react';
|
|
5
|
+
import { SceneNavigationModel } from './SceneNavigationModel';
|
|
6
|
+
|
|
7
|
+
const Context = createContext<SceneNavigationModel | undefined>(undefined);
|
|
8
|
+
|
|
9
|
+
export interface SceneNavigationModelProviderProps {
|
|
10
|
+
model: SceneNavigationModel;
|
|
11
|
+
children?: ReactNode;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Supplies the application structure and navigation every navigation region below it renders - the topbar and sidebar
|
|
16
|
+
* components of a shell find what to show here, so a screen document does not carry the application's navigation.
|
|
17
|
+
*/
|
|
18
|
+
export function SceneNavigationModelProvider({ model, children }: SceneNavigationModelProviderProps) {
|
|
19
|
+
return <Context.Provider value={model}>{children}</Context.Provider>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** The navigation model in scope, or undefined outside a {@link SceneNavigationModelProvider}. */
|
|
23
|
+
export function useSceneNavigationModel(): SceneNavigationModel | undefined {
|
|
24
|
+
return useContext(Context);
|
|
25
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { ApplicationStructureIndex } from '@cratis/scene.engine';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The module the screen on the page belongs to: the screen's qualified id starts with its module id, so `sales.orders`
|
|
8
|
+
* is in `sales`. Undefined for a screen outside every module - the application level.
|
|
9
|
+
*/
|
|
10
|
+
export function currentModuleOf(index: ApplicationStructureIndex, screen: string | undefined): string | undefined {
|
|
11
|
+
if (screen === undefined) return undefined;
|
|
12
|
+
const module = screen.split('.')[0];
|
|
13
|
+
return index.findModule(module) ? module : undefined;
|
|
14
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { ApplicationStructure, DestinationReference, NavigationRegionKind, NavigationModel } from '@cratis/scene.model';
|
|
5
|
+
import { ApplicationStructureIndex, EffectiveNavItem, effectiveNavigation } from '@cratis/scene.engine';
|
|
6
|
+
import { navigationItemDestination } from './navigationItemDestination';
|
|
7
|
+
|
|
8
|
+
function flatten(items: EffectiveNavItem[]): EffectiveNavItem[] {
|
|
9
|
+
return items.flatMap(item => [item, ...flatten(item.children ?? [])]);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Every destination the navigation of the application and of each module can lead to, once each: what a
|
|
14
|
+
* `SceneNavigationHost` is given as `destinations` so deep links to a navigation item resolve, and an item that renders
|
|
15
|
+
* into a nested outlet is placed with the screen that declares the outlet.
|
|
16
|
+
*/
|
|
17
|
+
export function navigationDestinations(
|
|
18
|
+
structure: ApplicationStructure | ApplicationStructureIndex,
|
|
19
|
+
navigation: NavigationModel,
|
|
20
|
+
screenFor?: (item: EffectiveNavItem) => string | undefined,
|
|
21
|
+
): DestinationReference[] {
|
|
22
|
+
const index = structure instanceof ApplicationStructureIndex ? structure : new ApplicationStructureIndex(structure);
|
|
23
|
+
const modules = [undefined, ...index.structure.modules.map(module => module.id)];
|
|
24
|
+
const seen = new Set<string>();
|
|
25
|
+
const destinations: DestinationReference[] = [];
|
|
26
|
+
|
|
27
|
+
for (const module of modules) {
|
|
28
|
+
for (const region of [NavigationRegionKind.Topbar, NavigationRegionKind.Sidebar]) {
|
|
29
|
+
for (const item of flatten(effectiveNavigation(index, navigation, module, region).items)) {
|
|
30
|
+
const destination = navigationItemDestination(item, screenFor);
|
|
31
|
+
if (destination === undefined) continue;
|
|
32
|
+
const key = JSON.stringify(destination);
|
|
33
|
+
if (seen.has(key)) continue;
|
|
34
|
+
seen.add(key);
|
|
35
|
+
destinations.push(destination);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return destinations;
|
|
41
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { EffectiveNavItem, primarySlot } from '@cratis/scene.engine';
|
|
5
|
+
import { defaultScreenFor } from './navigationItemDestination';
|
|
6
|
+
import { SceneNavigationContextValue } from '../SceneNavigationContext';
|
|
7
|
+
|
|
8
|
+
/** Where the navigation host is: the part of {@link SceneNavigationContextValue} that says what is on the page. */
|
|
9
|
+
export type NavigationPlacement = Pick<SceneNavigationContextValue, 'primaryScreen' | 'outlets'>;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Whether a navigation item is the one the page shows.
|
|
13
|
+
*
|
|
14
|
+
* An item is active when the screen it opens is the one in the slot it renders into: the primary region for the
|
|
15
|
+
* `content` slot, the nested outlet of that name otherwise. So several items can be active at once - each is shown in
|
|
16
|
+
* its own slot - and closing a slot's screen un-highlights its item. With `matchModule` - a topbar - an item that targets
|
|
17
|
+
* a module without a feature is also active while any screen of that module is in its slot.
|
|
18
|
+
*
|
|
19
|
+
* @param item The effective item.
|
|
20
|
+
* @param placement What the host currently shows.
|
|
21
|
+
* @param screenFor Names the screen an item opens; the feature the item resolves to when absent.
|
|
22
|
+
* @param matchModule Whether a module item is active on any screen of the module; a topbar's are, a sidebar's are not.
|
|
23
|
+
*/
|
|
24
|
+
export function navigationItemActive(
|
|
25
|
+
item: EffectiveNavItem,
|
|
26
|
+
placement: NavigationPlacement,
|
|
27
|
+
screenFor: (item: EffectiveNavItem) => string | undefined = defaultScreenFor,
|
|
28
|
+
matchModule = false,
|
|
29
|
+
): boolean {
|
|
30
|
+
const screen = screenFor(item);
|
|
31
|
+
if (screen === undefined) return false;
|
|
32
|
+
|
|
33
|
+
const shown = item.slot === primarySlot ? placement.primaryScreen : placement.outlets[item.slot];
|
|
34
|
+
if (shown === undefined) return false;
|
|
35
|
+
if (shown === screen) return true;
|
|
36
|
+
|
|
37
|
+
const target = item.target;
|
|
38
|
+
return matchModule && target !== undefined && target.feature === undefined && shown.startsWith(`${target.module}.`);
|
|
39
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { DestinationKind, DestinationReference } from '@cratis/scene.model';
|
|
5
|
+
import { EffectiveNavItem, primarySlot } from '@cratis/scene.engine';
|
|
6
|
+
|
|
7
|
+
/** The screen an item opens by default: the qualified id of the feature it resolves to. */
|
|
8
|
+
export function defaultScreenFor(item: EffectiveNavItem): string | undefined {
|
|
9
|
+
return item.target?.resolvedFeature;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Where activating a navigation item goes: its stable module/feature identity, the screen it opens, and the slot it
|
|
14
|
+
* renders into as the outlet - the primary `content` slot is the host's primary outlet.
|
|
15
|
+
*
|
|
16
|
+
* @param item The effective item.
|
|
17
|
+
* @param screenFor Names the screen an item opens; {@link defaultScreenFor} when absent.
|
|
18
|
+
* @returns The destination, or undefined when the item leads nowhere.
|
|
19
|
+
*/
|
|
20
|
+
export function navigationItemDestination(item: EffectiveNavItem, screenFor: (item: EffectiveNavItem) => string | undefined = defaultScreenFor): DestinationReference | undefined {
|
|
21
|
+
const screen = screenFor(item);
|
|
22
|
+
if (screen === undefined || item.target === undefined) return undefined;
|
|
23
|
+
|
|
24
|
+
const resolved = item.target.resolvedFeature;
|
|
25
|
+
const destination: DestinationReference = { kind: DestinationKind.Outlet, module: item.target.module, screen };
|
|
26
|
+
if (resolved !== undefined && resolved.startsWith(`${item.target.module}.`)) destination.feature = resolved.substring(item.target.module.length + 1);
|
|
27
|
+
if (item.slot !== primarySlot) destination.outlet = item.slot;
|
|
28
|
+
return destination;
|
|
29
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cratis/scene.react",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.29.0",
|
|
4
4
|
"description": "The React renderer implementing Scene.Engine's Renderer contract against real React/DOM.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cratis",
|
|
@@ -51,12 +51,12 @@
|
|
|
51
51
|
"build-storybook": "storybook build"
|
|
52
52
|
},
|
|
53
53
|
"dependencies": {
|
|
54
|
-
"@cratis/scene.engine": "4.
|
|
55
|
-
"@cratis/scene.model": "4.
|
|
54
|
+
"@cratis/scene.engine": "4.29.0",
|
|
55
|
+
"@cratis/scene.model": "4.29.0"
|
|
56
56
|
},
|
|
57
57
|
"devDependencies": {
|
|
58
|
-
"@cratis/scene.engine": "4.
|
|
59
|
-
"@cratis/scene.model": "4.
|
|
58
|
+
"@cratis/scene.engine": "4.29.0",
|
|
59
|
+
"@cratis/scene.model": "4.29.0",
|
|
60
60
|
"@storybook/addon-links": "^10.6.1",
|
|
61
61
|
"@storybook/react": "^10.6.1",
|
|
62
62
|
"@storybook/react-vite": "^10.6.1",
|