@olenbetong/synergi-react 4.2.6 → 4.3.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/es/colorScheme.d.ts +8 -0
- package/es/colorScheme.js +34 -0
- package/es/components/CodeEditor/CodeEditor.css +6 -0
- package/es/components/CodeEditor/CodeEditor.d.ts +35 -0
- package/es/components/CodeEditor/CodeEditor.js +114 -0
- package/es/components/CodeEditor/index.d.ts +1 -0
- package/es/components/CodeEditor/index.js +1 -0
- package/es/components/Toolbar/Toolbar.css +34 -21
- package/es/components/Toolbar/index.js +50 -18
- package/es/index.d.ts +1 -1
- package/es/index.js +1 -1
- package/es/mui/MuiThemeProvider.js +1 -31
- package/package.json +11 -6
- package/src/colorScheme.ts +44 -0
- package/src/components/CodeEditor/CodeEditor.css +6 -0
- package/src/components/CodeEditor/CodeEditor.tsx +173 -0
- package/src/components/CodeEditor/index.ts +1 -0
- package/src/components/Toolbar/Toolbar.css +34 -21
- package/src/components/Toolbar/index.tsx +66 -33
- package/src/index.ts +2 -0
- package/src/mui/MuiThemeProvider.tsx +1 -40
package/es/colorScheme.d.ts
CHANGED
|
@@ -15,3 +15,11 @@ export declare function getColorSchemePreference(): ColorSchemePreference;
|
|
|
15
15
|
export declare function setColorSchemePreference(preference: ColorSchemePreference): void;
|
|
16
16
|
/** Reads the stored preference and keeps it in sync with other toggles on the page. */
|
|
17
17
|
export declare function useColorSchemePreference(): [ColorSchemePreference, (value: ColorSchemePreference) => void];
|
|
18
|
+
/** The scheme Designsystemet is rendering with right now: the preference, with `auto` resolved. */
|
|
19
|
+
export declare function getResolvedColorScheme(): "light" | "dark";
|
|
20
|
+
/**
|
|
21
|
+
* Tracks the color scheme Designsystemet is rendering with. The article template
|
|
22
|
+
* sets `data-color-scheme` on `<body>`, so watch that attribute as well as the
|
|
23
|
+
* OS preference (used when the scheme is `auto`).
|
|
24
|
+
*/
|
|
25
|
+
export declare function useResolvedColorScheme(): "light" | "dark";
|
package/es/colorScheme.js
CHANGED
|
@@ -52,3 +52,37 @@ export function useColorSchemePreference() {
|
|
|
52
52
|
}, []);
|
|
53
53
|
return [preference, setValue];
|
|
54
54
|
}
|
|
55
|
+
/** The scheme Designsystemet is rendering with right now: the preference, with `auto` resolved. */
|
|
56
|
+
export function getResolvedColorScheme() {
|
|
57
|
+
if (typeof document === "undefined")
|
|
58
|
+
return "light";
|
|
59
|
+
let scheme = document.body?.dataset.colorScheme ?? document.documentElement?.dataset.colorScheme ?? "light";
|
|
60
|
+
if (scheme === "auto") {
|
|
61
|
+
return globalThis.matchMedia?.("(prefers-color-scheme: dark)").matches ? "dark" : "light";
|
|
62
|
+
}
|
|
63
|
+
return scheme === "dark" ? "dark" : "light";
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Tracks the color scheme Designsystemet is rendering with. The article template
|
|
67
|
+
* sets `data-color-scheme` on `<body>`, so watch that attribute as well as the
|
|
68
|
+
* OS preference (used when the scheme is `auto`).
|
|
69
|
+
*/
|
|
70
|
+
export function useResolvedColorScheme() {
|
|
71
|
+
let [scheme, setScheme] = useState(() => getResolvedColorScheme());
|
|
72
|
+
useEffect(() => {
|
|
73
|
+
function update() {
|
|
74
|
+
setScheme(getResolvedColorScheme());
|
|
75
|
+
}
|
|
76
|
+
update();
|
|
77
|
+
let observer = new MutationObserver(update);
|
|
78
|
+
observer.observe(document.body, { attributes: true, attributeFilter: ["data-color-scheme"] });
|
|
79
|
+
observer.observe(document.documentElement, { attributes: true, attributeFilter: ["data-color-scheme"] });
|
|
80
|
+
let media = globalThis.matchMedia?.("(prefers-color-scheme: dark)");
|
|
81
|
+
media?.addEventListener("change", update);
|
|
82
|
+
return () => {
|
|
83
|
+
observer.disconnect();
|
|
84
|
+
media?.removeEventListener("change", update);
|
|
85
|
+
};
|
|
86
|
+
}, []);
|
|
87
|
+
return scheme;
|
|
88
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import * as monaco from "monaco-editor";
|
|
2
|
+
import "./CodeEditor.css";
|
|
3
|
+
export type CodeEditorHandle = {
|
|
4
|
+
/** The Monaco editor instance, once mounted. */
|
|
5
|
+
editor: monaco.editor.IStandaloneCodeEditor | null;
|
|
6
|
+
/** Replaces the selection (or inserts at the cursor) and focuses the editor. */
|
|
7
|
+
insertText(text: string): void;
|
|
8
|
+
focus(): void;
|
|
9
|
+
};
|
|
10
|
+
export type CodeEditorProps = {
|
|
11
|
+
/** The text shown. The editor is controlled: `onChange` reports every edit. */
|
|
12
|
+
value: string;
|
|
13
|
+
onChange?: (value: string) => void;
|
|
14
|
+
/** Monaco language ID, e.g. `html`, `css`, `javascript`, `json`, `sql`, `xml`. */
|
|
15
|
+
language?: string;
|
|
16
|
+
/**
|
|
17
|
+
* Identifies the document. When it changes, the editor gets a fresh model, so undo history
|
|
18
|
+
* does not leak from one record to the next. Without it, a new `value` is applied as an edit
|
|
19
|
+
* that can be undone.
|
|
20
|
+
*/
|
|
21
|
+
docKey?: string;
|
|
22
|
+
readOnly?: boolean;
|
|
23
|
+
/** Ctrl/Cmd+S inside the editor. */
|
|
24
|
+
onSave?: () => void;
|
|
25
|
+
/** Extra Monaco options, applied on mount and whenever the object changes. */
|
|
26
|
+
options?: monaco.editor.IEditorOptions;
|
|
27
|
+
className?: string;
|
|
28
|
+
style?: React.CSSProperties;
|
|
29
|
+
"aria-label"?: string;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* Monaco code editor that follows the site's colour scheme. The element fills its parent's size
|
|
33
|
+
* through `className`/`style`; give it a height.
|
|
34
|
+
*/
|
|
35
|
+
export declare const CodeEditor: import("react").ForwardRefExoticComponent<CodeEditorProps & import("react").RefAttributes<CodeEditorHandle>>;
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import clsx from "clsx";
|
|
3
|
+
import * as monaco from "monaco-editor";
|
|
4
|
+
import { forwardRef, useEffect, useImperativeHandle, useRef } from "react";
|
|
5
|
+
import { useResolvedColorScheme } from "../../colorScheme.js";
|
|
6
|
+
import "./CodeEditor.css";
|
|
7
|
+
/**
|
|
8
|
+
* Monaco code editor that follows the site's colour scheme. The element fills its parent's size
|
|
9
|
+
* through `className`/`style`; give it a height.
|
|
10
|
+
*/
|
|
11
|
+
export const CodeEditor = forwardRef(function CodeEditor({ value, onChange, language = "plaintext", docKey, readOnly = false, onSave, options, className, style, "aria-label": ariaLabel, }, ref) {
|
|
12
|
+
let container = useRef(null);
|
|
13
|
+
let editor = useRef(null);
|
|
14
|
+
// Latest callbacks without re-creating the editor.
|
|
15
|
+
let callbacks = useRef({ onChange, onSave });
|
|
16
|
+
callbacks.current = { onChange, onSave };
|
|
17
|
+
// Set while the editor applies a `value` from props, so that edit is not reported back.
|
|
18
|
+
let applyingValue = useRef(false);
|
|
19
|
+
let shownDoc = useRef(undefined);
|
|
20
|
+
let colorScheme = useResolvedColorScheme();
|
|
21
|
+
useEffect(() => {
|
|
22
|
+
if (!container.current)
|
|
23
|
+
return;
|
|
24
|
+
let instance = monaco.editor.create(container.current, {
|
|
25
|
+
value,
|
|
26
|
+
language,
|
|
27
|
+
theme: colorScheme === "dark" ? "vs-dark" : "vs",
|
|
28
|
+
automaticLayout: true,
|
|
29
|
+
minimap: { enabled: false },
|
|
30
|
+
scrollBeyondLastLine: false,
|
|
31
|
+
fixedOverflowWidgets: true,
|
|
32
|
+
ariaLabel,
|
|
33
|
+
readOnly,
|
|
34
|
+
...options,
|
|
35
|
+
});
|
|
36
|
+
editor.current = instance;
|
|
37
|
+
shownDoc.current = docKey;
|
|
38
|
+
let changeListener = instance.onDidChangeModelContent(() => {
|
|
39
|
+
if (!applyingValue.current) {
|
|
40
|
+
callbacks.current.onChange?.(instance.getValue());
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
instance.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyS, () => callbacks.current.onSave?.());
|
|
44
|
+
return () => {
|
|
45
|
+
changeListener.dispose();
|
|
46
|
+
instance.getModel()?.dispose();
|
|
47
|
+
instance.dispose();
|
|
48
|
+
editor.current = null;
|
|
49
|
+
};
|
|
50
|
+
// The editor is created once; every prop is synced by the effects below.
|
|
51
|
+
// oxlint-disable-next-line react-hooks/exhaustive-deps
|
|
52
|
+
}, []);
|
|
53
|
+
useEffect(() => {
|
|
54
|
+
let instance = editor.current;
|
|
55
|
+
if (!instance)
|
|
56
|
+
return;
|
|
57
|
+
applyingValue.current = true;
|
|
58
|
+
try {
|
|
59
|
+
if (shownDoc.current !== docKey) {
|
|
60
|
+
// New document: a new model, so undo history stays with the document it belongs to.
|
|
61
|
+
let old = instance.getModel();
|
|
62
|
+
instance.setModel(monaco.editor.createModel(value, language));
|
|
63
|
+
old?.dispose();
|
|
64
|
+
shownDoc.current = docKey;
|
|
65
|
+
}
|
|
66
|
+
else if (instance.getValue() !== value) {
|
|
67
|
+
// Same document, changed from outside (cancel, reload): one undoable edit.
|
|
68
|
+
let model = instance.getModel();
|
|
69
|
+
if (model) {
|
|
70
|
+
instance.executeEdits("external", [{ range: model.getFullModelRange(), text: value }]);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
finally {
|
|
75
|
+
applyingValue.current = false;
|
|
76
|
+
}
|
|
77
|
+
}, [docKey, value, language]);
|
|
78
|
+
useEffect(() => {
|
|
79
|
+
let model = editor.current?.getModel();
|
|
80
|
+
if (model && model.getLanguageId() !== language) {
|
|
81
|
+
monaco.editor.setModelLanguage(model, language);
|
|
82
|
+
}
|
|
83
|
+
}, [language]);
|
|
84
|
+
useEffect(() => {
|
|
85
|
+
editor.current?.updateOptions({ readOnly, ariaLabel, ...options });
|
|
86
|
+
}, [readOnly, ariaLabel, options]);
|
|
87
|
+
useEffect(() => {
|
|
88
|
+
// The theme is global to every editor on the page.
|
|
89
|
+
monaco.editor.setTheme(colorScheme === "dark" ? "vs-dark" : "vs");
|
|
90
|
+
}, [colorScheme]);
|
|
91
|
+
useImperativeHandle(ref, () => ({
|
|
92
|
+
get editor() {
|
|
93
|
+
return editor.current;
|
|
94
|
+
},
|
|
95
|
+
insertText(text) {
|
|
96
|
+
let instance = editor.current;
|
|
97
|
+
if (!instance)
|
|
98
|
+
return;
|
|
99
|
+
let range = instance.getSelection() ?? new monaco.Range(1, 1, 1, 1);
|
|
100
|
+
instance.executeEdits("insert", [{ range, text, forceMoveMarkers: true }]);
|
|
101
|
+
instance.focus();
|
|
102
|
+
},
|
|
103
|
+
focus() {
|
|
104
|
+
editor.current?.focus();
|
|
105
|
+
},
|
|
106
|
+
}));
|
|
107
|
+
/*
|
|
108
|
+
* `monaco-component`: Monaco renders the context menu in a shadow root attached to this
|
|
109
|
+
* container, a parent of `.monaco-editor`. Its colours are theme variables Monaco defines on
|
|
110
|
+
* `.monaco-editor, .monaco-diff-editor, .monaco-component` only, so without the class the
|
|
111
|
+
* menu inherits none of them and has no background.
|
|
112
|
+
*/
|
|
113
|
+
return _jsx("div", { ref: container, className: clsx("ObCodeEditor-root", "monaco-component", className), style: style });
|
|
114
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { CodeEditor, type CodeEditorHandle, type CodeEditorProps } from "./CodeEditor.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { CodeEditor } from "./CodeEditor.js";
|
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
|
|
20
20
|
.ob-breadcrumbs__item {
|
|
21
21
|
display: flex;
|
|
22
|
+
flex: 0 0 auto;
|
|
22
23
|
align-items: center;
|
|
23
24
|
font-size: var(--ds-font-size-100, 0.875rem);
|
|
24
25
|
white-space: nowrap;
|
|
@@ -54,8 +55,6 @@
|
|
|
54
55
|
}
|
|
55
56
|
|
|
56
57
|
.ob-breadcrumbs__overflow {
|
|
57
|
-
position: relative;
|
|
58
|
-
|
|
59
58
|
button {
|
|
60
59
|
background: none;
|
|
61
60
|
border: 1px solid transparent;
|
|
@@ -86,39 +85,53 @@
|
|
|
86
85
|
}
|
|
87
86
|
}
|
|
88
87
|
|
|
88
|
+
/*
|
|
89
|
+
* The crumbs there is no room for, behind the "…" button. A popover, so it sits in the top
|
|
90
|
+
* layer where the toolbar cannot clip it; placed under the button by the component. The
|
|
91
|
+
* toolbar's contrast colour is inherited through the DOM even there, so the menu sets its own.
|
|
92
|
+
*/
|
|
89
93
|
.ob-breadcrumbs__overflow-menu {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
box-shadow: var(--ds-shadow-overlay, 0 4px 8px rgba(0, 0, 0, 0.16));
|
|
94
|
-
left: 0;
|
|
95
|
-
list-style: none;
|
|
96
|
-
margin: var(--ds-space-050, 0.25rem) 0 0;
|
|
94
|
+
position: fixed;
|
|
95
|
+
inset: auto;
|
|
96
|
+
box-sizing: border-box;
|
|
97
97
|
min-width: 10rem;
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
98
|
+
max-width: calc(100vw - 1rem);
|
|
99
|
+
margin: 0;
|
|
100
|
+
padding: var(--ds-size-1, 0.25rem) 0;
|
|
101
|
+
list-style: none;
|
|
102
|
+
border: 1px solid var(--ds-color-neutral-border-subtle, #cbd0d6);
|
|
103
|
+
border-radius: var(--ds-border-radius-md, 4px);
|
|
104
|
+
background: var(--ds-color-neutral-background-default, #fff);
|
|
105
|
+
color: var(--ds-color-neutral-text-default, #1e2b3c);
|
|
106
|
+
box-shadow: var(--ds-shadow-md, 0 4px 8px rgb(0 0 0 / 16%));
|
|
107
|
+
font-size: var(--ds-body-sm-font-size, 0.875rem);
|
|
102
108
|
|
|
103
109
|
li {
|
|
104
110
|
a,
|
|
105
111
|
span {
|
|
106
|
-
color: var(--ds-text, inherit);
|
|
107
112
|
display: block;
|
|
108
|
-
|
|
109
|
-
|
|
113
|
+
padding: var(--ds-size-2, 0.5rem) var(--ds-size-4, 1rem);
|
|
114
|
+
overflow: hidden;
|
|
115
|
+
color: inherit;
|
|
110
116
|
text-decoration: none;
|
|
117
|
+
text-overflow: ellipsis;
|
|
111
118
|
white-space: nowrap;
|
|
119
|
+
}
|
|
112
120
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
color: var(--ds-text, inherit);
|
|
116
|
-
}
|
|
121
|
+
a:hover {
|
|
122
|
+
background: var(--ds-color-neutral-surface-hover, rgb(0 0 0 / 6%));
|
|
117
123
|
}
|
|
118
124
|
|
|
119
125
|
a:focus-visible {
|
|
120
|
-
outline: 2px solid var(--ds-
|
|
126
|
+
outline: 2px solid var(--ds-color-focus-outer, currentColor);
|
|
121
127
|
outline-offset: -2px;
|
|
122
128
|
}
|
|
123
129
|
}
|
|
130
|
+
|
|
131
|
+
/* Touch sized rows where there is no pointer to aim with. */
|
|
132
|
+
@media (pointer: coarse) {
|
|
133
|
+
li :is(a, span) {
|
|
134
|
+
padding-block: var(--ds-size-3, 0.75rem);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
124
137
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
2
|
import "./Toolbar.css";
|
|
3
3
|
import { MenuHamburgerIcon } from "@navikt/aksel-icons";
|
|
4
|
-
import { useEffect, useLayoutEffect, useRef, useState } from "react";
|
|
4
|
+
import { useEffect, useId, useLayoutEffect, useRef, useState } from "react";
|
|
5
5
|
import { createPortal } from "react-dom";
|
|
6
6
|
import { Link } from "react-router";
|
|
7
7
|
import { getLocalizedString } from "../../i18n.js";
|
|
@@ -86,19 +86,30 @@ function BreadcrumbLink({ item }) {
|
|
|
86
86
|
}
|
|
87
87
|
function BreadcrumbsInner({ items }) {
|
|
88
88
|
let [firstVisible, setFirstVisible] = useState(0);
|
|
89
|
-
let [
|
|
89
|
+
let [menuOpen, setMenuOpen] = useState(false);
|
|
90
90
|
let navRef = useRef(null);
|
|
91
91
|
let listRef = useRef(null);
|
|
92
|
-
let
|
|
92
|
+
let buttonRef = useRef(null);
|
|
93
|
+
let menuRef = useRef(null);
|
|
94
|
+
let menuId = useId();
|
|
95
|
+
// A new trail (the user navigated) is measured from scratch. Adjusted during render, so
|
|
96
|
+
// the previous page's collapse never paints on the new one.
|
|
97
|
+
let trail = items.map((item) => item.label).join("\n");
|
|
98
|
+
let [measuredTrail, setMeasuredTrail] = useState(trail);
|
|
99
|
+
if (measuredTrail !== trail) {
|
|
100
|
+
setMeasuredTrail(trail);
|
|
101
|
+
setFirstVisible(0);
|
|
102
|
+
}
|
|
93
103
|
// Detect overflow: increment firstVisible until the list fits within the nav.
|
|
94
104
|
// Runs after every render so it keeps pushing items into overflow until the
|
|
95
105
|
// list stops overflowing or only the last item (current page) remains.
|
|
96
106
|
useLayoutEffect(() => {
|
|
97
|
-
let nav = navRef.current;
|
|
98
107
|
let list = listRef.current;
|
|
99
|
-
if (!
|
|
108
|
+
if (!list)
|
|
100
109
|
return;
|
|
101
|
-
|
|
110
|
+
// Against the list's own box, not the nav's: the nav also holds the separator the site
|
|
111
|
+
// stylesheet draws between the brand and the trail.
|
|
112
|
+
if (list.scrollWidth > list.clientWidth + 1 && firstVisible < items.length - 1) {
|
|
102
113
|
setFirstVisible((prev) => prev + 1);
|
|
103
114
|
}
|
|
104
115
|
});
|
|
@@ -108,27 +119,48 @@ function BreadcrumbsInner({ items }) {
|
|
|
108
119
|
let nav = navRef.current;
|
|
109
120
|
if (!nav)
|
|
110
121
|
return;
|
|
122
|
+
let width = nav.clientWidth;
|
|
111
123
|
let observer = new ResizeObserver(() => {
|
|
124
|
+
// The first callback reports the size the trail was just measured at.
|
|
125
|
+
if (nav.clientWidth === width)
|
|
126
|
+
return;
|
|
127
|
+
width = nav.clientWidth;
|
|
112
128
|
setFirstVisible(0);
|
|
113
129
|
});
|
|
114
130
|
observer.observe(nav);
|
|
115
131
|
return () => observer.disconnect();
|
|
116
132
|
}, []);
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
133
|
+
/*
|
|
134
|
+
* The menu is a popover: it renders in the top layer, so the toolbar and the trail, which
|
|
135
|
+
* both clip their overflow, cannot cut it off, and the browser closes it on Escape and on a
|
|
136
|
+
* click outside. The top layer has no positioned ancestor, so it is placed under the button
|
|
137
|
+
* by hand, and pulled back in when it would run past the edge of a narrow screen.
|
|
138
|
+
*/
|
|
139
|
+
function placeMenu() {
|
|
140
|
+
let button = buttonRef.current;
|
|
141
|
+
let menu = menuRef.current;
|
|
142
|
+
if (!button || !menu)
|
|
120
143
|
return;
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
return () => document.removeEventListener("mousedown", handleClickOutside);
|
|
128
|
-
}, [overflowOpen]);
|
|
144
|
+
let rect = button.getBoundingClientRect();
|
|
145
|
+
let margin = 8;
|
|
146
|
+
let left = Math.min(rect.left, window.innerWidth - menu.offsetWidth - margin);
|
|
147
|
+
menu.style.top = `${rect.bottom + 4}px`;
|
|
148
|
+
menu.style.left = `${Math.max(left, margin)}px`;
|
|
149
|
+
}
|
|
129
150
|
let hiddenItems = items.slice(0, firstVisible);
|
|
130
151
|
let visibleItems = items.slice(firstVisible);
|
|
131
|
-
return (_jsx("nav", { className: "ob-breadcrumbs", "aria-label": "Breadcrumb", ref: navRef, children: _jsxs("ol", { className: "ob-breadcrumbs__list", ref: listRef, children: [firstVisible > 0 && (_jsxs("li", { className: "ob-breadcrumbs__item ob-breadcrumbs__overflow",
|
|
152
|
+
return (_jsx("nav", { className: "ob-breadcrumbs", "aria-label": "Breadcrumb", ref: navRef, children: _jsxs("ol", { className: "ob-breadcrumbs__list", ref: listRef, children: [firstVisible > 0 && (_jsxs("li", { className: "ob-breadcrumbs__item ob-breadcrumbs__overflow", children: [_jsx("button", { ref: buttonRef, type: "button", "aria-label": getLocalizedString("Show hidden breadcrumbs"), "aria-expanded": menuOpen, popoverTarget: menuId, children: "\u2026" }), _jsx("ul", { ref: menuRef, id: menuId, popover: "auto", className: "ob-breadcrumbs__overflow-menu",
|
|
153
|
+
// Placed before it shows, so it never paints in the popover's default
|
|
154
|
+
// spot, and again once it has a width to keep it on the screen.
|
|
155
|
+
onBeforeToggle: (event) => {
|
|
156
|
+
if (event.newState === "open")
|
|
157
|
+
placeMenu();
|
|
158
|
+
}, onToggle: (event) => {
|
|
159
|
+
let open = event.newState === "open";
|
|
160
|
+
setMenuOpen(open);
|
|
161
|
+
if (open)
|
|
162
|
+
placeMenu();
|
|
163
|
+
}, children: hiddenItems.map((item, index) => (_jsx("li", { onClick: () => menuRef.current?.hidePopover(), children: _jsx(BreadcrumbLink, { item: item }) }, index))) })] })), visibleItems.map((item, index) => {
|
|
132
164
|
let actualIndex = firstVisible + index;
|
|
133
165
|
let isCurrent = actualIndex === items.length - 1;
|
|
134
166
|
return (_jsx("li", { className: "ob-breadcrumbs__item", children: isCurrent ? _jsx("span", { "aria-current": "page", children: item.label }) : _jsx(BreadcrumbLink, { item: item }) }, actualIndex));
|
package/es/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export { AppShell, type AppShellProps, hasSiteChrome } from "./chrome/AppShell/index.js";
|
|
2
2
|
export { Checkbox, type CheckboxProps } from "./components/Checkbox/index.js";
|
|
3
|
-
export { COLOR_SCHEME_CHANGE_EVENT, type ColorSchemePreference, getColorSchemePreference, setColorSchemePreference, useColorSchemePreference, } from "./colorScheme.js";
|
|
3
|
+
export { COLOR_SCHEME_CHANGE_EVENT, type ColorSchemePreference, getColorSchemePreference, getResolvedColorScheme, setColorSchemePreference, useColorSchemePreference, useResolvedColorScheme, } from "./colorScheme.js";
|
|
4
4
|
export { CardColumn, CardDetail, CardLabel, CardStep, CardStepList, ColorCard } from "./components/ColorCard/index.js";
|
|
5
5
|
export { DateNavigator } from "./components/DateNavigator/index.js";
|
|
6
6
|
export * from "./components/FilePond.js";
|
package/es/index.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// loaded once per page by the site template rather than bundled into every app.
|
|
3
3
|
export { AppShell, hasSiteChrome } from "./chrome/AppShell/index.js";
|
|
4
4
|
export { Checkbox } from "./components/Checkbox/index.js";
|
|
5
|
-
export { COLOR_SCHEME_CHANGE_EVENT, getColorSchemePreference, setColorSchemePreference, useColorSchemePreference, } from "./colorScheme.js";
|
|
5
|
+
export { COLOR_SCHEME_CHANGE_EVENT, getColorSchemePreference, getResolvedColorScheme, setColorSchemePreference, useColorSchemePreference, useResolvedColorScheme, } from "./colorScheme.js";
|
|
6
6
|
export { CardColumn, CardDetail, CardLabel, CardStep, CardStepList, ColorCard } from "./components/ColorCard/index.js";
|
|
7
7
|
export { DateNavigator } from "./components/DateNavigator/index.js";
|
|
8
8
|
export * from "./components/FilePond.js";
|
|
@@ -2,37 +2,7 @@ import { jsx as _jsx } from "react/jsx-runtime";
|
|
|
2
2
|
import { StyledEngineProvider, ThemeProvider } from "@mui/material";
|
|
3
3
|
import { createTheme as muiCreateTheme } from "@mui/material/styles";
|
|
4
4
|
import { useEffect, useState } from "react";
|
|
5
|
-
|
|
6
|
-
let scheme = document.body?.dataset.colorScheme ?? document.documentElement?.dataset.colorScheme ?? "light";
|
|
7
|
-
if (scheme === "auto") {
|
|
8
|
-
return globalThis.matchMedia?.("(prefers-color-scheme: dark)").matches ? "dark" : "light";
|
|
9
|
-
}
|
|
10
|
-
return scheme === "dark" ? "dark" : "light";
|
|
11
|
-
}
|
|
12
|
-
/**
|
|
13
|
-
* Tracks the color scheme Designsystemet is rendering with. The article template
|
|
14
|
-
* sets `data-color-scheme` on `<body>`, so watch that attribute as well as the
|
|
15
|
-
* OS preference (used when the scheme is `auto`).
|
|
16
|
-
*/
|
|
17
|
-
function useColorScheme() {
|
|
18
|
-
let [scheme, setScheme] = useState(() => getColorScheme());
|
|
19
|
-
useEffect(() => {
|
|
20
|
-
function update() {
|
|
21
|
-
setScheme(getColorScheme());
|
|
22
|
-
}
|
|
23
|
-
update();
|
|
24
|
-
let observer = new MutationObserver(update);
|
|
25
|
-
observer.observe(document.body, { attributes: true, attributeFilter: ["data-color-scheme"] });
|
|
26
|
-
observer.observe(document.documentElement, { attributes: true, attributeFilter: ["data-color-scheme"] });
|
|
27
|
-
let media = globalThis.matchMedia?.("(prefers-color-scheme: dark)");
|
|
28
|
-
media?.addEventListener("change", update);
|
|
29
|
-
return () => {
|
|
30
|
-
observer.disconnect();
|
|
31
|
-
media?.removeEventListener("change", update);
|
|
32
|
-
};
|
|
33
|
-
}, []);
|
|
34
|
-
return scheme;
|
|
35
|
-
}
|
|
5
|
+
import { useResolvedColorScheme as useColorScheme } from "../colorScheme.js";
|
|
36
6
|
function buildMuiTheme(mode) {
|
|
37
7
|
const s = getComputedStyle(document.body);
|
|
38
8
|
const get = (v) => s.getPropertyValue(v).trim();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@olenbetong/synergi-react",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.3.0",
|
|
4
4
|
"description": "Standalone React component for SynergiWeb and Partner Portal",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -19,6 +19,10 @@
|
|
|
19
19
|
"./pdf": {
|
|
20
20
|
"types": "./es/components/PdfViewer/index.d.ts",
|
|
21
21
|
"import": "./es/components/PdfViewer/index.js"
|
|
22
|
+
},
|
|
23
|
+
"./code-editor": {
|
|
24
|
+
"types": "./es/components/CodeEditor/index.d.ts",
|
|
25
|
+
"import": "./es/components/CodeEditor/index.js"
|
|
22
26
|
}
|
|
23
27
|
},
|
|
24
28
|
"files": [
|
|
@@ -39,11 +43,11 @@
|
|
|
39
43
|
"@emotion/styled": "^11.14.1",
|
|
40
44
|
"@mui/icons-material": "9.4.0",
|
|
41
45
|
"@mui/material": "9.4.0",
|
|
42
|
-
"@olenbetong/appframe-core": "2.15.
|
|
43
|
-
"@olenbetong/appframe-data": "1.8.2",
|
|
44
|
-
"@olenbetong/appframe-ds": "1.
|
|
45
|
-
"@olenbetong/appframe-mui": "7.0.
|
|
46
|
-
"@olenbetong/appframe-react": "2.2.7",
|
|
46
|
+
"@olenbetong/appframe-core": "^2.15.2",
|
|
47
|
+
"@olenbetong/appframe-data": "^1.8.2",
|
|
48
|
+
"@olenbetong/appframe-ds": "^1.4.1",
|
|
49
|
+
"@olenbetong/appframe-mui": "^7.0.10",
|
|
50
|
+
"@olenbetong/appframe-react": "^2.2.7",
|
|
47
51
|
"clsx": "^2.1.1",
|
|
48
52
|
"filepond": "^4.32.12",
|
|
49
53
|
"filepond-plugin-file-poster": "^2.5.2",
|
|
@@ -53,6 +57,7 @@
|
|
|
53
57
|
"filepond-plugin-image-transform": "^3.8.8",
|
|
54
58
|
"localforage": "^1.10.0",
|
|
55
59
|
"mitt": "^3.0.1",
|
|
60
|
+
"monaco-editor": "0.57.0",
|
|
56
61
|
"pdf-lib": "1.17.1",
|
|
57
62
|
"pdfjs-dist": "5.7.284",
|
|
58
63
|
"react": "19.3.0",
|
package/src/colorScheme.ts
CHANGED
|
@@ -79,3 +79,47 @@ export function useColorSchemePreference(): [ColorSchemePreference, (value: Colo
|
|
|
79
79
|
|
|
80
80
|
return [preference, setValue];
|
|
81
81
|
}
|
|
82
|
+
|
|
83
|
+
/** The scheme Designsystemet is rendering with right now: the preference, with `auto` resolved. */
|
|
84
|
+
export function getResolvedColorScheme(): "light" | "dark" {
|
|
85
|
+
if (typeof document === "undefined") return "light";
|
|
86
|
+
|
|
87
|
+
let scheme = document.body?.dataset.colorScheme ?? document.documentElement?.dataset.colorScheme ?? "light";
|
|
88
|
+
|
|
89
|
+
if (scheme === "auto") {
|
|
90
|
+
return globalThis.matchMedia?.("(prefers-color-scheme: dark)").matches ? "dark" : "light";
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return scheme === "dark" ? "dark" : "light";
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Tracks the color scheme Designsystemet is rendering with. The article template
|
|
98
|
+
* sets `data-color-scheme` on `<body>`, so watch that attribute as well as the
|
|
99
|
+
* OS preference (used when the scheme is `auto`).
|
|
100
|
+
*/
|
|
101
|
+
export function useResolvedColorScheme(): "light" | "dark" {
|
|
102
|
+
let [scheme, setScheme] = useState<"light" | "dark">(() => getResolvedColorScheme());
|
|
103
|
+
|
|
104
|
+
useEffect(() => {
|
|
105
|
+
function update() {
|
|
106
|
+
setScheme(getResolvedColorScheme());
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
update();
|
|
110
|
+
|
|
111
|
+
let observer = new MutationObserver(update);
|
|
112
|
+
observer.observe(document.body, { attributes: true, attributeFilter: ["data-color-scheme"] });
|
|
113
|
+
observer.observe(document.documentElement, { attributes: true, attributeFilter: ["data-color-scheme"] });
|
|
114
|
+
|
|
115
|
+
let media = globalThis.matchMedia?.("(prefers-color-scheme: dark)");
|
|
116
|
+
media?.addEventListener("change", update);
|
|
117
|
+
|
|
118
|
+
return () => {
|
|
119
|
+
observer.disconnect();
|
|
120
|
+
media?.removeEventListener("change", update);
|
|
121
|
+
};
|
|
122
|
+
}, []);
|
|
123
|
+
|
|
124
|
+
return scheme;
|
|
125
|
+
}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import clsx from "clsx";
|
|
2
|
+
import * as monaco from "monaco-editor";
|
|
3
|
+
import { forwardRef, useEffect, useImperativeHandle, useRef } from "react";
|
|
4
|
+
|
|
5
|
+
import { useResolvedColorScheme } from "../../colorScheme.js";
|
|
6
|
+
import "./CodeEditor.css";
|
|
7
|
+
|
|
8
|
+
/*
|
|
9
|
+
* Monaco on the 2026 templates comes from the platform bundle through the import map. The
|
|
10
|
+
* platform entry sets `MonacoEnvironment` (the editor and language workers, served next to it)
|
|
11
|
+
* and links Monaco's stylesheet on first import, so this component only drives the editor.
|
|
12
|
+
* Off the platform (the Vite dev server, a legacy template) Monaco is bundled from npm: its
|
|
13
|
+
* stylesheets come with it, and without `MonacoEnvironment` the language services run on the
|
|
14
|
+
* main thread with a console warning. That is fine for development; it is why this lives in its
|
|
15
|
+
* own entry point and is not re-exported from the package root.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
export type CodeEditorHandle = {
|
|
19
|
+
/** The Monaco editor instance, once mounted. */
|
|
20
|
+
editor: monaco.editor.IStandaloneCodeEditor | null;
|
|
21
|
+
/** Replaces the selection (or inserts at the cursor) and focuses the editor. */
|
|
22
|
+
insertText(text: string): void;
|
|
23
|
+
focus(): void;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export type CodeEditorProps = {
|
|
27
|
+
/** The text shown. The editor is controlled: `onChange` reports every edit. */
|
|
28
|
+
value: string;
|
|
29
|
+
onChange?: (value: string) => void;
|
|
30
|
+
/** Monaco language ID, e.g. `html`, `css`, `javascript`, `json`, `sql`, `xml`. */
|
|
31
|
+
language?: string;
|
|
32
|
+
/**
|
|
33
|
+
* Identifies the document. When it changes, the editor gets a fresh model, so undo history
|
|
34
|
+
* does not leak from one record to the next. Without it, a new `value` is applied as an edit
|
|
35
|
+
* that can be undone.
|
|
36
|
+
*/
|
|
37
|
+
docKey?: string;
|
|
38
|
+
readOnly?: boolean;
|
|
39
|
+
/** Ctrl/Cmd+S inside the editor. */
|
|
40
|
+
onSave?: () => void;
|
|
41
|
+
/** Extra Monaco options, applied on mount and whenever the object changes. */
|
|
42
|
+
options?: monaco.editor.IEditorOptions;
|
|
43
|
+
className?: string;
|
|
44
|
+
style?: React.CSSProperties;
|
|
45
|
+
"aria-label"?: string;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Monaco code editor that follows the site's colour scheme. The element fills its parent's size
|
|
50
|
+
* through `className`/`style`; give it a height.
|
|
51
|
+
*/
|
|
52
|
+
export const CodeEditor = forwardRef<CodeEditorHandle, CodeEditorProps>(function CodeEditor(
|
|
53
|
+
{
|
|
54
|
+
value,
|
|
55
|
+
onChange,
|
|
56
|
+
language = "plaintext",
|
|
57
|
+
docKey,
|
|
58
|
+
readOnly = false,
|
|
59
|
+
onSave,
|
|
60
|
+
options,
|
|
61
|
+
className,
|
|
62
|
+
style,
|
|
63
|
+
"aria-label": ariaLabel,
|
|
64
|
+
},
|
|
65
|
+
ref,
|
|
66
|
+
) {
|
|
67
|
+
let container = useRef<HTMLDivElement>(null);
|
|
68
|
+
let editor = useRef<monaco.editor.IStandaloneCodeEditor | null>(null);
|
|
69
|
+
// Latest callbacks without re-creating the editor.
|
|
70
|
+
let callbacks = useRef({ onChange, onSave });
|
|
71
|
+
callbacks.current = { onChange, onSave };
|
|
72
|
+
// Set while the editor applies a `value` from props, so that edit is not reported back.
|
|
73
|
+
let applyingValue = useRef(false);
|
|
74
|
+
let shownDoc = useRef<string | undefined>(undefined);
|
|
75
|
+
let colorScheme = useResolvedColorScheme();
|
|
76
|
+
|
|
77
|
+
useEffect(() => {
|
|
78
|
+
if (!container.current) return;
|
|
79
|
+
let instance = monaco.editor.create(container.current, {
|
|
80
|
+
value,
|
|
81
|
+
language,
|
|
82
|
+
theme: colorScheme === "dark" ? "vs-dark" : "vs",
|
|
83
|
+
automaticLayout: true,
|
|
84
|
+
minimap: { enabled: false },
|
|
85
|
+
scrollBeyondLastLine: false,
|
|
86
|
+
fixedOverflowWidgets: true,
|
|
87
|
+
ariaLabel,
|
|
88
|
+
readOnly,
|
|
89
|
+
...options,
|
|
90
|
+
});
|
|
91
|
+
editor.current = instance;
|
|
92
|
+
shownDoc.current = docKey;
|
|
93
|
+
let changeListener = instance.onDidChangeModelContent(() => {
|
|
94
|
+
if (!applyingValue.current) {
|
|
95
|
+
callbacks.current.onChange?.(instance.getValue());
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
instance.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyS, () => callbacks.current.onSave?.());
|
|
99
|
+
|
|
100
|
+
return () => {
|
|
101
|
+
changeListener.dispose();
|
|
102
|
+
instance.getModel()?.dispose();
|
|
103
|
+
instance.dispose();
|
|
104
|
+
editor.current = null;
|
|
105
|
+
};
|
|
106
|
+
// The editor is created once; every prop is synced by the effects below.
|
|
107
|
+
// oxlint-disable-next-line react-hooks/exhaustive-deps
|
|
108
|
+
}, []);
|
|
109
|
+
|
|
110
|
+
useEffect(() => {
|
|
111
|
+
let instance = editor.current;
|
|
112
|
+
if (!instance) return;
|
|
113
|
+
|
|
114
|
+
applyingValue.current = true;
|
|
115
|
+
try {
|
|
116
|
+
if (shownDoc.current !== docKey) {
|
|
117
|
+
// New document: a new model, so undo history stays with the document it belongs to.
|
|
118
|
+
let old = instance.getModel();
|
|
119
|
+
instance.setModel(monaco.editor.createModel(value, language));
|
|
120
|
+
old?.dispose();
|
|
121
|
+
shownDoc.current = docKey;
|
|
122
|
+
} else if (instance.getValue() !== value) {
|
|
123
|
+
// Same document, changed from outside (cancel, reload): one undoable edit.
|
|
124
|
+
let model = instance.getModel();
|
|
125
|
+
if (model) {
|
|
126
|
+
instance.executeEdits("external", [{ range: model.getFullModelRange(), text: value }]);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
} finally {
|
|
130
|
+
applyingValue.current = false;
|
|
131
|
+
}
|
|
132
|
+
}, [docKey, value, language]);
|
|
133
|
+
|
|
134
|
+
useEffect(() => {
|
|
135
|
+
let model = editor.current?.getModel();
|
|
136
|
+
if (model && model.getLanguageId() !== language) {
|
|
137
|
+
monaco.editor.setModelLanguage(model, language);
|
|
138
|
+
}
|
|
139
|
+
}, [language]);
|
|
140
|
+
|
|
141
|
+
useEffect(() => {
|
|
142
|
+
editor.current?.updateOptions({ readOnly, ariaLabel, ...options });
|
|
143
|
+
}, [readOnly, ariaLabel, options]);
|
|
144
|
+
|
|
145
|
+
useEffect(() => {
|
|
146
|
+
// The theme is global to every editor on the page.
|
|
147
|
+
monaco.editor.setTheme(colorScheme === "dark" ? "vs-dark" : "vs");
|
|
148
|
+
}, [colorScheme]);
|
|
149
|
+
|
|
150
|
+
useImperativeHandle(ref, () => ({
|
|
151
|
+
get editor() {
|
|
152
|
+
return editor.current;
|
|
153
|
+
},
|
|
154
|
+
insertText(text: string) {
|
|
155
|
+
let instance = editor.current;
|
|
156
|
+
if (!instance) return;
|
|
157
|
+
let range = instance.getSelection() ?? new monaco.Range(1, 1, 1, 1);
|
|
158
|
+
instance.executeEdits("insert", [{ range, text, forceMoveMarkers: true }]);
|
|
159
|
+
instance.focus();
|
|
160
|
+
},
|
|
161
|
+
focus() {
|
|
162
|
+
editor.current?.focus();
|
|
163
|
+
},
|
|
164
|
+
}));
|
|
165
|
+
|
|
166
|
+
/*
|
|
167
|
+
* `monaco-component`: Monaco renders the context menu in a shadow root attached to this
|
|
168
|
+
* container, a parent of `.monaco-editor`. Its colours are theme variables Monaco defines on
|
|
169
|
+
* `.monaco-editor, .monaco-diff-editor, .monaco-component` only, so without the class the
|
|
170
|
+
* menu inherits none of them and has no background.
|
|
171
|
+
*/
|
|
172
|
+
return <div ref={container} className={clsx("ObCodeEditor-root", "monaco-component", className)} style={style} />;
|
|
173
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { CodeEditor, type CodeEditorHandle, type CodeEditorProps } from "./CodeEditor.js";
|
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
|
|
20
20
|
.ob-breadcrumbs__item {
|
|
21
21
|
display: flex;
|
|
22
|
+
flex: 0 0 auto;
|
|
22
23
|
align-items: center;
|
|
23
24
|
font-size: var(--ds-font-size-100, 0.875rem);
|
|
24
25
|
white-space: nowrap;
|
|
@@ -54,8 +55,6 @@
|
|
|
54
55
|
}
|
|
55
56
|
|
|
56
57
|
.ob-breadcrumbs__overflow {
|
|
57
|
-
position: relative;
|
|
58
|
-
|
|
59
58
|
button {
|
|
60
59
|
background: none;
|
|
61
60
|
border: 1px solid transparent;
|
|
@@ -86,39 +85,53 @@
|
|
|
86
85
|
}
|
|
87
86
|
}
|
|
88
87
|
|
|
88
|
+
/*
|
|
89
|
+
* The crumbs there is no room for, behind the "…" button. A popover, so it sits in the top
|
|
90
|
+
* layer where the toolbar cannot clip it; placed under the button by the component. The
|
|
91
|
+
* toolbar's contrast colour is inherited through the DOM even there, so the menu sets its own.
|
|
92
|
+
*/
|
|
89
93
|
.ob-breadcrumbs__overflow-menu {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
box-shadow: var(--ds-shadow-overlay, 0 4px 8px rgba(0, 0, 0, 0.16));
|
|
94
|
-
left: 0;
|
|
95
|
-
list-style: none;
|
|
96
|
-
margin: var(--ds-space-050, 0.25rem) 0 0;
|
|
94
|
+
position: fixed;
|
|
95
|
+
inset: auto;
|
|
96
|
+
box-sizing: border-box;
|
|
97
97
|
min-width: 10rem;
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
98
|
+
max-width: calc(100vw - 1rem);
|
|
99
|
+
margin: 0;
|
|
100
|
+
padding: var(--ds-size-1, 0.25rem) 0;
|
|
101
|
+
list-style: none;
|
|
102
|
+
border: 1px solid var(--ds-color-neutral-border-subtle, #cbd0d6);
|
|
103
|
+
border-radius: var(--ds-border-radius-md, 4px);
|
|
104
|
+
background: var(--ds-color-neutral-background-default, #fff);
|
|
105
|
+
color: var(--ds-color-neutral-text-default, #1e2b3c);
|
|
106
|
+
box-shadow: var(--ds-shadow-md, 0 4px 8px rgb(0 0 0 / 16%));
|
|
107
|
+
font-size: var(--ds-body-sm-font-size, 0.875rem);
|
|
102
108
|
|
|
103
109
|
li {
|
|
104
110
|
a,
|
|
105
111
|
span {
|
|
106
|
-
color: var(--ds-text, inherit);
|
|
107
112
|
display: block;
|
|
108
|
-
|
|
109
|
-
|
|
113
|
+
padding: var(--ds-size-2, 0.5rem) var(--ds-size-4, 1rem);
|
|
114
|
+
overflow: hidden;
|
|
115
|
+
color: inherit;
|
|
110
116
|
text-decoration: none;
|
|
117
|
+
text-overflow: ellipsis;
|
|
111
118
|
white-space: nowrap;
|
|
119
|
+
}
|
|
112
120
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
color: var(--ds-text, inherit);
|
|
116
|
-
}
|
|
121
|
+
a:hover {
|
|
122
|
+
background: var(--ds-color-neutral-surface-hover, rgb(0 0 0 / 6%));
|
|
117
123
|
}
|
|
118
124
|
|
|
119
125
|
a:focus-visible {
|
|
120
|
-
outline: 2px solid var(--ds-
|
|
126
|
+
outline: 2px solid var(--ds-color-focus-outer, currentColor);
|
|
121
127
|
outline-offset: -2px;
|
|
122
128
|
}
|
|
123
129
|
}
|
|
130
|
+
|
|
131
|
+
/* Touch sized rows where there is no pointer to aim with. */
|
|
132
|
+
@media (pointer: coarse) {
|
|
133
|
+
li :is(a, span) {
|
|
134
|
+
padding-block: var(--ds-size-3, 0.75rem);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
124
137
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import "./Toolbar.css";
|
|
2
2
|
|
|
3
3
|
import { MenuHamburgerIcon } from "@navikt/aksel-icons";
|
|
4
|
-
import { useEffect, useLayoutEffect, useRef, useState } from "react";
|
|
4
|
+
import { useEffect, useId, useLayoutEffect, useRef, useState } from "react";
|
|
5
5
|
import { createPortal } from "react-dom";
|
|
6
6
|
import { Link, type To } from "react-router";
|
|
7
7
|
import { getLocalizedString } from "../../i18n.js";
|
|
@@ -133,20 +133,32 @@ export type ToolbarBreadcrumbsProps = {
|
|
|
133
133
|
|
|
134
134
|
function BreadcrumbsInner({ items }: ToolbarBreadcrumbsProps) {
|
|
135
135
|
let [firstVisible, setFirstVisible] = useState(0);
|
|
136
|
-
let [
|
|
136
|
+
let [menuOpen, setMenuOpen] = useState(false);
|
|
137
137
|
let navRef = useRef<HTMLElement>(null);
|
|
138
138
|
let listRef = useRef<HTMLOListElement>(null);
|
|
139
|
-
let
|
|
139
|
+
let buttonRef = useRef<HTMLButtonElement>(null);
|
|
140
|
+
let menuRef = useRef<HTMLUListElement>(null);
|
|
141
|
+
let menuId = useId();
|
|
142
|
+
|
|
143
|
+
// A new trail (the user navigated) is measured from scratch. Adjusted during render, so
|
|
144
|
+
// the previous page's collapse never paints on the new one.
|
|
145
|
+
let trail = items.map((item) => item.label).join("\n");
|
|
146
|
+
let [measuredTrail, setMeasuredTrail] = useState(trail);
|
|
147
|
+
if (measuredTrail !== trail) {
|
|
148
|
+
setMeasuredTrail(trail);
|
|
149
|
+
setFirstVisible(0);
|
|
150
|
+
}
|
|
140
151
|
|
|
141
152
|
// Detect overflow: increment firstVisible until the list fits within the nav.
|
|
142
153
|
// Runs after every render so it keeps pushing items into overflow until the
|
|
143
154
|
// list stops overflowing or only the last item (current page) remains.
|
|
144
155
|
useLayoutEffect(() => {
|
|
145
|
-
let nav = navRef.current;
|
|
146
156
|
let list = listRef.current;
|
|
147
|
-
if (!
|
|
157
|
+
if (!list) return;
|
|
148
158
|
|
|
149
|
-
|
|
159
|
+
// Against the list's own box, not the nav's: the nav also holds the separator the site
|
|
160
|
+
// stylesheet draws between the brand and the trail.
|
|
161
|
+
if (list.scrollWidth > list.clientWidth + 1 && firstVisible < items.length - 1) {
|
|
150
162
|
setFirstVisible((prev) => prev + 1);
|
|
151
163
|
}
|
|
152
164
|
});
|
|
@@ -157,7 +169,11 @@ function BreadcrumbsInner({ items }: ToolbarBreadcrumbsProps) {
|
|
|
157
169
|
let nav = navRef.current;
|
|
158
170
|
if (!nav) return;
|
|
159
171
|
|
|
172
|
+
let width = nav.clientWidth;
|
|
160
173
|
let observer = new ResizeObserver(() => {
|
|
174
|
+
// The first callback reports the size the trail was just measured at.
|
|
175
|
+
if (nav.clientWidth === width) return;
|
|
176
|
+
width = nav.clientWidth;
|
|
161
177
|
setFirstVisible(0);
|
|
162
178
|
});
|
|
163
179
|
|
|
@@ -165,19 +181,23 @@ function BreadcrumbsInner({ items }: ToolbarBreadcrumbsProps) {
|
|
|
165
181
|
return () => observer.disconnect();
|
|
166
182
|
}, []);
|
|
167
183
|
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
184
|
+
/*
|
|
185
|
+
* The menu is a popover: it renders in the top layer, so the toolbar and the trail, which
|
|
186
|
+
* both clip their overflow, cannot cut it off, and the browser closes it on Escape and on a
|
|
187
|
+
* click outside. The top layer has no positioned ancestor, so it is placed under the button
|
|
188
|
+
* by hand, and pulled back in when it would run past the edge of a narrow screen.
|
|
189
|
+
*/
|
|
190
|
+
function placeMenu() {
|
|
191
|
+
let button = buttonRef.current;
|
|
192
|
+
let menu = menuRef.current;
|
|
193
|
+
if (!button || !menu) return;
|
|
177
194
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
195
|
+
let rect = button.getBoundingClientRect();
|
|
196
|
+
let margin = 8;
|
|
197
|
+
let left = Math.min(rect.left, window.innerWidth - menu.offsetWidth - margin);
|
|
198
|
+
menu.style.top = `${rect.bottom + 4}px`;
|
|
199
|
+
menu.style.left = `${Math.max(left, margin)}px`;
|
|
200
|
+
}
|
|
181
201
|
|
|
182
202
|
let hiddenItems = items.slice(0, firstVisible);
|
|
183
203
|
let visibleItems = items.slice(firstVisible);
|
|
@@ -186,26 +206,39 @@ function BreadcrumbsInner({ items }: ToolbarBreadcrumbsProps) {
|
|
|
186
206
|
<nav className="ob-breadcrumbs" aria-label="Breadcrumb" ref={navRef}>
|
|
187
207
|
<ol className="ob-breadcrumbs__list" ref={listRef}>
|
|
188
208
|
{firstVisible > 0 && (
|
|
189
|
-
<li className="ob-breadcrumbs__item ob-breadcrumbs__overflow"
|
|
209
|
+
<li className="ob-breadcrumbs__item ob-breadcrumbs__overflow">
|
|
190
210
|
<button
|
|
211
|
+
ref={buttonRef}
|
|
191
212
|
type="button"
|
|
192
|
-
aria-label="Show hidden breadcrumbs"
|
|
193
|
-
aria-expanded={
|
|
194
|
-
|
|
195
|
-
onClick={() => setOverflowOpen((prev) => !prev)}
|
|
213
|
+
aria-label={getLocalizedString("Show hidden breadcrumbs")}
|
|
214
|
+
aria-expanded={menuOpen}
|
|
215
|
+
popoverTarget={menuId}
|
|
196
216
|
>
|
|
197
217
|
…
|
|
198
218
|
</button>
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
219
|
+
<ul
|
|
220
|
+
ref={menuRef}
|
|
221
|
+
id={menuId}
|
|
222
|
+
popover="auto"
|
|
223
|
+
className="ob-breadcrumbs__overflow-menu"
|
|
224
|
+
// Placed before it shows, so it never paints in the popover's default
|
|
225
|
+
// spot, and again once it has a width to keep it on the screen.
|
|
226
|
+
onBeforeToggle={(event) => {
|
|
227
|
+
if (event.newState === "open") placeMenu();
|
|
228
|
+
}}
|
|
229
|
+
onToggle={(event) => {
|
|
230
|
+
let open = event.newState === "open";
|
|
231
|
+
setMenuOpen(open);
|
|
232
|
+
if (open) placeMenu();
|
|
233
|
+
}}
|
|
234
|
+
>
|
|
235
|
+
{hiddenItems.map((item, index) => (
|
|
236
|
+
// oxlint-disable-next-line react/no-array-index-key -- stable list with no reordering
|
|
237
|
+
<li key={index} onClick={() => menuRef.current?.hidePopover()}>
|
|
238
|
+
<BreadcrumbLink item={item} />
|
|
239
|
+
</li>
|
|
240
|
+
))}
|
|
241
|
+
</ul>
|
|
209
242
|
</li>
|
|
210
243
|
)}
|
|
211
244
|
{visibleItems.map((item, index) => {
|
package/src/index.ts
CHANGED
|
@@ -6,8 +6,10 @@ export {
|
|
|
6
6
|
COLOR_SCHEME_CHANGE_EVENT,
|
|
7
7
|
type ColorSchemePreference,
|
|
8
8
|
getColorSchemePreference,
|
|
9
|
+
getResolvedColorScheme,
|
|
9
10
|
setColorSchemePreference,
|
|
10
11
|
useColorSchemePreference,
|
|
12
|
+
useResolvedColorScheme,
|
|
11
13
|
} from "./colorScheme.js";
|
|
12
14
|
export { CardColumn, CardDetail, CardLabel, CardStep, CardStepList, ColorCard } from "./components/ColorCard/index.js";
|
|
13
15
|
export { DateNavigator } from "./components/DateNavigator/index.js";
|
|
@@ -2,46 +2,7 @@ import { StyledEngineProvider, ThemeProvider } from "@mui/material";
|
|
|
2
2
|
import { createTheme as muiCreateTheme } from "@mui/material/styles";
|
|
3
3
|
import { type ReactNode, useEffect, useState } from "react";
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
let scheme = document.body?.dataset.colorScheme ?? document.documentElement?.dataset.colorScheme ?? "light";
|
|
7
|
-
|
|
8
|
-
if (scheme === "auto") {
|
|
9
|
-
return globalThis.matchMedia?.("(prefers-color-scheme: dark)").matches ? "dark" : "light";
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
return scheme === "dark" ? "dark" : "light";
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Tracks the color scheme Designsystemet is rendering with. The article template
|
|
17
|
-
* sets `data-color-scheme` on `<body>`, so watch that attribute as well as the
|
|
18
|
-
* OS preference (used when the scheme is `auto`).
|
|
19
|
-
*/
|
|
20
|
-
function useColorScheme() {
|
|
21
|
-
let [scheme, setScheme] = useState<"light" | "dark">(() => getColorScheme());
|
|
22
|
-
|
|
23
|
-
useEffect(() => {
|
|
24
|
-
function update() {
|
|
25
|
-
setScheme(getColorScheme());
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
update();
|
|
29
|
-
|
|
30
|
-
let observer = new MutationObserver(update);
|
|
31
|
-
observer.observe(document.body, { attributes: true, attributeFilter: ["data-color-scheme"] });
|
|
32
|
-
observer.observe(document.documentElement, { attributes: true, attributeFilter: ["data-color-scheme"] });
|
|
33
|
-
|
|
34
|
-
let media = globalThis.matchMedia?.("(prefers-color-scheme: dark)");
|
|
35
|
-
media?.addEventListener("change", update);
|
|
36
|
-
|
|
37
|
-
return () => {
|
|
38
|
-
observer.disconnect();
|
|
39
|
-
media?.removeEventListener("change", update);
|
|
40
|
-
};
|
|
41
|
-
}, []);
|
|
42
|
-
|
|
43
|
-
return scheme;
|
|
44
|
-
}
|
|
5
|
+
import { useResolvedColorScheme as useColorScheme } from "../colorScheme.js";
|
|
45
6
|
|
|
46
7
|
function buildMuiTheme(mode: "light" | "dark") {
|
|
47
8
|
const s = getComputedStyle(document.body);
|