@public-ui/sample-react 4.4.0 → 4.4.1-7b9d0237557064506ca27572fd67a6d3955cdf19.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/README.md CHANGED
@@ -23,6 +23,12 @@ The sample is built with **Vite** for fast development and production builds.
23
23
  - `src/scenarios` – cross component scenarios
24
24
  - `src/shares` – shared utilities
25
25
 
26
+ ## Color scheme
27
+
28
+ The Sidebar carries a **Color scheme** select with the states Auto, Light and Dark. Light and Dark set the plain CSS `color-scheme` property on `<html>`; Auto removes it again so the host app's own `:root { color-scheme: light dark }` applies and the operating system decides. Because `color-scheme` is inherited and inheritance crosses the shadow boundary, the page and every KoliBri component follow the same value. The choice applies without a reload and is remembered in `localStorage`, and `?colorScheme=dark` on a route sets it from a link.
29
+
30
+ The mechanics live in `src/shares/colorScheme.ts` and `src/hooks/useColorScheme.ts`, modelled on the visual block outline next to them. Because this package is mounted by both the presentation app and the visual-test host app, the select appears in both.
31
+
26
32
  ## Installation and usage
27
33
 
28
34
  ```bash
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@public-ui/sample-react",
3
- "version": "4.4.0",
3
+ "version": "4.4.1-7b9d0237557064506ca27572fd67a6d3955cdf19.0",
4
4
  "description": "This app contains samples for the KoliBri/Public UI",
5
5
  "type": "module",
6
6
  "license": "EUPL-1.2",
@@ -20,8 +20,8 @@
20
20
  "typescript": "5.9.3",
21
21
  "world_countries_lists": "3.3.0",
22
22
  "zod": "4.4.3",
23
- "@public-ui/react-hook-form-adapter": "4.4.0",
24
- "@public-ui/react-v19": "4.4.0"
23
+ "@public-ui/react-hook-form-adapter": "4.4.1-7b9d0237557064506ca27572fd67a6d3955cdf19.0",
24
+ "@public-ui/react-v19": "4.4.1-7b9d0237557064506ca27572fd67a6d3955cdf19.0"
25
25
  },
26
26
  "devDependencies": {
27
27
  "@types/react": "19.2.18",
@@ -37,7 +37,7 @@
37
37
  "react": "19.2.8",
38
38
  "react-dom": "19.2.8",
39
39
  "react-router": "8.3.0",
40
- "@public-ui/components": "4.4.0"
40
+ "@public-ui/components": "4.4.1-7b9d0237557064506ca27572fd67a6d3955cdf19.0"
41
41
  },
42
42
  "files": [
43
43
  "assets",
package/src/App.tsx CHANGED
@@ -6,13 +6,14 @@ import PackageJson from '@public-ui/components/package.json';
6
6
 
7
7
  import { BackPage } from './components/BackPage';
8
8
  import { Sidebar } from './components/Sidebar';
9
+ import { useColorScheme } from './hooks/useColorScheme';
9
10
  import { useSetCurrentLocation } from './hooks/useSetCurrentLocation';
10
11
  import { useVisualBlockOutline } from './hooks/useVisualBlockOutline';
11
12
  import { HideMenusContext } from './shares/HideMenusContext';
12
13
  import { ROUTES } from './shares/routes';
13
14
  import { sampleAppDataService } from './shares/sampleAppDataService';
14
15
  import { getTheme, getThemeName, setRegisteredThemes, setStorage, setTheme } from './shares/store';
15
- import { PUBLIC_THEMES, UNSTYLED_THEME } from './shares/theme';
16
+ import { DARK_CAPABLE_THEMES, PUBLIC_THEMES, UNSTYLED_THEME } from './shares/theme';
16
17
 
17
18
  import type { Route as MyRoute, Routes as MyRoutes } from './shares/types';
18
19
 
@@ -142,6 +143,8 @@ export const App: FC<Props> = ({ customThemes }) => {
142
143
  setTheme(theme); // set for `getTheme` usages within the application
143
144
  useSetCurrentLocation();
144
145
  useVisualBlockOutline();
146
+ const supportsDark = DARK_CAPABLE_THEMES.includes(theme);
147
+ const [colorScheme, setColorScheme] = useColorScheme(supportsDark);
145
148
 
146
149
  useEffect(() => {
147
150
  document.title = `KoliBri-Handout - ${getThemeName(getTheme())} | v${PackageJson.version}`;
@@ -150,7 +153,12 @@ export const App: FC<Props> = ({ customThemes }) => {
150
153
  }, [theme]);
151
154
 
152
155
  const handleThemeChange = (theme: unknown) => {
153
- setSearchParams({ theme: theme as string });
156
+ /* The updater form keeps the other parameters – replacing the object would drop `hideMenus`,
157
+ `visualBlocks` and `colorScheme` on every theme change. */
158
+ setSearchParams((previous) => {
159
+ previous.set('theme', theme as string);
160
+ return previous;
161
+ });
154
162
  window.location.reload();
155
163
  };
156
164
 
@@ -177,7 +185,10 @@ export const App: FC<Props> = ({ customThemes }) => {
177
185
  routeList={ROUTE_LIST}
178
186
  buildDate={process.env.BUILD_DATE}
179
187
  commitHash={process.env.COMMIT_HASH}
188
+ colorScheme={colorScheme}
189
+ supportsDark={supportsDark}
180
190
  onThemeChange={handleThemeChange}
191
+ onColorSchemeChange={setColorScheme}
181
192
  />
182
193
  )}
183
194
 
@@ -6,10 +6,13 @@ import { useMobile } from '../hooks/useMobile';
6
6
 
7
7
  import type { SelectOption } from '@public-ui/components';
8
8
  import { useLocation } from 'react-router';
9
+ import type { ColorSchemePreference } from '../shares/colorScheme';
9
10
  import type { Theme } from '../shares/theme';
10
11
  import type { Routes } from '../shares/types';
11
12
  import Navigation from './Navigation';
12
13
 
14
+ const getThemeLabel = (themes: Theme[], theme: string) => themes.find((candidate) => candidate.key === theme)?.name ?? theme;
15
+
13
16
  type BuildInformationProps = {
14
17
  buildDate?: string | null;
15
18
  commitHash?: string | null;
@@ -46,10 +49,32 @@ type Props = {
46
49
  sample: string;
47
50
  buildDate?: string;
48
51
  commitHash?: string;
52
+ colorScheme: ColorSchemePreference;
53
+ supportsDark: boolean;
49
54
  onThemeChange: (theme: unknown) => void;
55
+ onColorSchemeChange: (colorScheme: ColorSchemePreference) => void;
50
56
  };
51
57
 
52
- export const Sidebar: FC<Props> = ({ version, themes, theme, routes, routeList, sample, buildDate, commitHash, onThemeChange }) => {
58
+ const COLOR_SCHEME_OPTIONS: SelectOption<ColorSchemePreference>[] = [
59
+ { label: 'Auto (follow the OS)', value: 'auto' },
60
+ { label: 'Light', value: 'light' },
61
+ { label: 'Dark (untested)', value: 'dark' },
62
+ ];
63
+
64
+ export const Sidebar: FC<Props> = ({
65
+ version,
66
+ themes,
67
+ theme,
68
+ routes,
69
+ routeList,
70
+ sample,
71
+ buildDate,
72
+ commitHash,
73
+ colorScheme,
74
+ supportsDark,
75
+ onThemeChange,
76
+ onColorSchemeChange,
77
+ }) => {
53
78
  /* KolSelect calls onChange initially by design - work around this with a state variable */
54
79
 
55
80
  const getIndexOfSample = () => routeList.indexOf(sample);
@@ -59,6 +84,14 @@ export const Sidebar: FC<Props> = ({ version, themes, theme, routes, routeList,
59
84
  onThemeChange(value as string);
60
85
  };
61
86
 
87
+ const handleColorSchemeSelectChange = (_event: Event, value: unknown) => {
88
+ onColorSchemeChange(value as ColorSchemePreference);
89
+ };
90
+
91
+ /* A theme without a dark palette is pinned to light by `useColorScheme`. Disabling the select
92
+ says so at a glance; the hint explains why, for anyone who wonders. */
93
+ const colorSchemeHint = supportsDark ? undefined : `The ${getThemeLabel(themes, theme)} theme has no dark palette yet, so the scheme stays light.`;
94
+
62
95
  const handlePreviousClick = () => {
63
96
  const currentIndex = getIndexOfSample();
64
97
  const nextIndex = currentIndex <= 0 ? routeList.length - 1 : currentIndex - 1;
@@ -100,6 +133,19 @@ export const Sidebar: FC<Props> = ({ version, themes, theme, routes, routeList,
100
133
  </div>
101
134
  <BuildInformation buildDate={buildDate} commitHash={commitHash} />
102
135
  {!isMobile ? <KolSelect _label="Theme" _options={themeOption} _on={{ onChange: handleThemeSelectChange }} _value={theme} class="mt"></KolSelect> : ''}
136
+ {!isMobile ? (
137
+ <KolSelect
138
+ _label="Color scheme"
139
+ _disabled={!supportsDark}
140
+ _hint={colorSchemeHint}
141
+ _options={COLOR_SCHEME_OPTIONS}
142
+ _on={{ onChange: handleColorSchemeSelectChange }}
143
+ _value={colorScheme}
144
+ class="mt"
145
+ ></KolSelect>
146
+ ) : (
147
+ ''
148
+ )}
103
149
  {!isMobile ? <KolHeading _label="Components" _level={2} className="block mt"></KolHeading> : ''}
104
150
  <div className="flex flex-justify-between flex-items-center mt">
105
151
  <KolButton _icons="kolicon-chevron-left" _hideLabel _label="Previous component" _on={{ onClick: handlePreviousClick }} />
@@ -122,6 +168,15 @@ export const Sidebar: FC<Props> = ({ version, themes, theme, routes, routeList,
122
168
  {isMobile ? (
123
169
  <KolDrawer _align="top" _label="Navigation" _hasCloser={true} ref={drawerElement}>
124
170
  <KolSelect _label="Theme" _options={themeOption} _on={{ onChange: handleThemeSelectChange }} _value={theme} class="mt"></KolSelect>
171
+ <KolSelect
172
+ _label="Color scheme"
173
+ _disabled={!supportsDark}
174
+ _hint={colorSchemeHint}
175
+ _options={COLOR_SCHEME_OPTIONS}
176
+ _on={{ onChange: handleColorSchemeSelectChange }}
177
+ _value={colorScheme}
178
+ class="mt"
179
+ ></KolSelect>
125
180
  <KolHeading _label="Components" _level={2} className="block mt"></KolHeading>
126
181
  <Navigation routes={routes} />
127
182
  </KolDrawer>
@@ -152,10 +152,12 @@ export const InputDateCopyPaste: React.FC = () => {
152
152
  }}
153
153
  />
154
154
 
155
- <small className="opacity-80">Click the button to copy the exact German date, then paste it into the date field below.</small>
155
+ <p className="opacity-80" id="button-desc">
156
+ Click the button to copy the exact German date, then paste it into the date field below.
157
+ </p>
156
158
 
157
159
  <div className="flex items-center gap-2">
158
- <KolButton _label="Copy to Clipboard" _on={{ onClick: () => copyToClipboard(deValue) }} />
160
+ <KolButton _label="Copy to Clipboard" _on={{ onClick: () => copyToClipboard(deValue) }} aria-describedby="button-desc" />
159
161
  {!isoFromDe && <span className="text-red-600">Invalid date</span>}
160
162
  </div>
161
163
 
@@ -19,7 +19,6 @@ const HEADERS = {
19
19
  const PROPS = {
20
20
  _data: DATA,
21
21
  _headers: HEADERS,
22
- _headerCells: HEADERS,
23
22
  _label: 'Label should always be filled',
24
23
  _minWidth: 'auto',
25
24
  };
@@ -49,7 +49,7 @@ export const InteractiveChildElements: FC = () => (
49
49
  <section className="w-full flex flex-col">
50
50
  <KolTableStateless
51
51
  _label="Button styles"
52
- _headerCells={{
52
+ _headers={{
53
53
  horizontal: [
54
54
  [
55
55
  getButtonHeaderCell('primary'),
@@ -87,7 +87,7 @@ export const InteractiveChildElements: FC = () => (
87
87
 
88
88
  <KolTableStateless
89
89
  _label="Link styles"
90
- _headerCells={{
90
+ _headers={{
91
91
  horizontal: [
92
92
  [
93
93
  {
@@ -18,6 +18,7 @@ import { PredefinedSettings } from './predefined-settings';
18
18
  import { TableRenderCell } from './render-cell';
19
19
  import { TableSettingsColumnOptions } from './settings-column-options';
20
20
  import { TableSortData } from './sort-data';
21
+ import { TableStateColumns } from './state-columns';
21
22
  import { TableStatefulAsync } from './stateful-async';
22
23
  import { TableStatefulExport } from './stateful-export';
23
24
  import { TableStatefulResetSort } from './stateful-reset-sort';
@@ -49,6 +50,7 @@ export const TABLE_ROUTES: Routes = {
49
50
  'predefined-settings': PredefinedSettings,
50
51
  'action-columns': TableActionColumns,
51
52
  'action-columns-performance': TableActionColumnPerformance,
53
+ 'state-columns': TableStateColumns,
52
54
  'render-cell': TableRenderCell,
53
55
  'settings-column-options': TableSettingsColumnOptions,
54
56
  'sort-data': TableSortData,
@@ -0,0 +1,79 @@
1
+ import type { KoliBriTableHeaderCellWithLogic, StateColumnPropType } from '@public-ui/components';
2
+ import { KolTableStateful } from '@public-ui/react-v19';
3
+ import type { FC } from 'react';
4
+ import React from 'react';
5
+ import { SampleBlock } from '../SampleBlock';
6
+ import { SampleDescription } from '../SampleDescription';
7
+
8
+ type ProjectTask = {
9
+ id: string;
10
+ project: string;
11
+ owner: string;
12
+ userStates: StateColumnPropType[];
13
+ };
14
+
15
+ const HEADERS: { horizontal: KoliBriTableHeaderCellWithLogic[][] } = {
16
+ horizontal: [
17
+ [
18
+ { key: 'id', label: 'ID', width: 80 },
19
+ { key: 'project', label: 'Project' },
20
+ { key: 'owner', label: 'Owner', width: 140 },
21
+ {
22
+ type: 'state',
23
+ key: 'userStates',
24
+ label: 'States',
25
+ textAlign: 'left',
26
+ width: 500,
27
+ },
28
+ ],
29
+ ],
30
+ };
31
+
32
+ const DATA: ProjectTask[] = [
33
+ {
34
+ id: 'T-01',
35
+ project: 'Onboarding checklist',
36
+ owner: 'Alex Rivera',
37
+ userStates: [
38
+ {
39
+ type: 'badge',
40
+ _label: 'Logged out',
41
+ _color: '#aa0000',
42
+ },
43
+ {
44
+ type: 'badge',
45
+ _label: 'In Project',
46
+ _color: '#0000aa',
47
+ },
48
+ ],
49
+ },
50
+ {
51
+ id: 'T-02',
52
+ project: 'Accessibility audit',
53
+ owner: 'Jamie Chen',
54
+ userStates: [
55
+ {
56
+ type: 'badge',
57
+ _label: 'Logged in',
58
+ _color: '#00aa00',
59
+ },
60
+ {
61
+ type: 'badge',
62
+ _label: 'In Project',
63
+ _color: '#0000aa',
64
+ },
65
+ ],
66
+ },
67
+ ];
68
+
69
+ export const TableStateColumns: FC = () => (
70
+ <>
71
+ <SampleDescription>
72
+ <p>Simple example using the state column: States are defined once in the column header definition using a factory function.</p>
73
+ </SampleDescription>
74
+
75
+ <SampleBlock id="action-columns" className="w-full">
76
+ <KolTableStateful _label="Tasks with action buttons" _headers={HEADERS} _data={DATA} className="block" />
77
+ </SampleBlock>
78
+ </>
79
+ );
@@ -50,7 +50,7 @@ export const TableStatelessAsync: FC = () => {
50
50
  <KolTableStateless
51
51
  _label="Table for demonstration purposes"
52
52
  _loading={loading}
53
- _headerCells={HEADERS_HORIZONTAL}
53
+ _headers={HEADERS_HORIZONTAL}
54
54
  _data={complexData}
55
55
  _on={{
56
56
  onSort: () => loadData(),
@@ -76,7 +76,7 @@ export const TableStatelessWithSelection: FC = () => {
76
76
  <SampleBlock id="selection" className="w-full">
77
77
  <KolTableStateless
78
78
  _label="Table with selection checkboxes"
79
- _headerCells={{
79
+ _headers={{
80
80
  horizontal: [
81
81
  [
82
82
  { key: 'id', label: '#ID', textAlign: 'left' },
@@ -51,7 +51,7 @@ export const TableStatelessWithSettingsMenu: FC = () => (
51
51
  _label="Users (stateless with settings menu)"
52
52
  className="w-full"
53
53
  _hasSettingsMenu={true}
54
- _headerCells={{
54
+ _headers={{
55
55
  horizontal: [
56
56
  [
57
57
  {
@@ -76,7 +76,7 @@ export const TableStatelessWithSingleSelection: FC = () => {
76
76
  <SampleBlock id="single-selection" className="w-full" fitContent>
77
77
  <KolTableStateless
78
78
  _label="Table with selection checkboxes"
79
- _headerCells={{
79
+ _headers={{
80
80
  horizontal: [
81
81
  [
82
82
  { key: 'id', label: '#ID', textAlign: 'left' },
@@ -15,7 +15,7 @@ export const TableStateless: FC = () => (
15
15
  <SampleBlock id="stateless" className="w-full" fitContent>
16
16
  <KolTableStateless
17
17
  _label="Table for demonstration purposes"
18
- _headerCells={{
18
+ _headers={{
19
19
  horizontal: [
20
20
  [
21
21
  { key: 'left', label: 'left', textAlign: 'left', sortDirection: 'ASC' },
@@ -0,0 +1,56 @@
1
+ import { useCallback, useEffect, useRef, useState } from 'react';
2
+ import { useSearchParams } from 'react-router';
3
+
4
+ import { applyColorScheme, isColorSchemePreference, persistColorScheme, readStoredColorScheme } from '../shares/colorScheme';
5
+
6
+ import type { ColorSchemePreference } from '../shares/colorScheme';
7
+
8
+ const SEARCH_PARAM = 'colorScheme';
9
+
10
+ /**
11
+ * Makes the color scheme (see `shares/colorScheme`) switchable at runtime:
12
+ * - `?colorScheme=dark` opens a route in dark mode right away – combinable with `?hideMenus`,
13
+ * - the Sidebar's select changes it without a reload and remembers the choice for the next visit.
14
+ *
15
+ * Without the URL parameter the remembered choice applies; the parameter always wins.
16
+ *
17
+ * `supportsDark` says whether the active theme ships a dark palette. It does not: the scheme is
18
+ * forced to light, because such a theme would otherwise render light components on a page that
19
+ * follows the scheme. The remembered choice stays untouched and applies again as soon as a theme
20
+ * with a dark palette is selected.
21
+ *
22
+ * Returns the scheme that is actually on the document, which is what the Sidebar's select shows.
23
+ */
24
+ export const useColorScheme = (supportsDark: boolean): [ColorSchemePreference, (preference: ColorSchemePreference) => void] => {
25
+ const [searchParams] = useSearchParams();
26
+ const param = searchParams.get(SEARCH_PARAM);
27
+ const [preference, setPreference] = useState<ColorSchemePreference>(() => (isColorSchemePreference(param) ? param : readStoredColorScheme()));
28
+
29
+ useEffect(() => {
30
+ /* The URL wins over the stored value but must not overwrite it – reading a link should not
31
+ change what the next visit without a parameter shows. */
32
+ setPreference(isColorSchemePreference(param) ? param : readStoredColorScheme());
33
+ }, [param]);
34
+
35
+ const scheme: ColorSchemePreference = supportsDark ? preference : 'light';
36
+
37
+ useEffect(() => {
38
+ applyColorScheme(scheme);
39
+ }, [scheme]);
40
+
41
+ const schemeRef = useRef(scheme);
42
+ schemeRef.current = scheme;
43
+
44
+ const choose = useCallback((next: ColorSchemePreference) => {
45
+ /* KolSelect emits onChange once while mounting, carrying the value it was given. Ignoring a
46
+ value that is already the active one keeps that echo from persisting a preference the user
47
+ never chose – which matters most while a theme without a dark palette forces light. */
48
+ if (schemeRef.current === next) {
49
+ return;
50
+ }
51
+ setPreference(next);
52
+ persistColorScheme(next);
53
+ }, []);
54
+
55
+ return [scheme, choose];
56
+ };
@@ -0,0 +1,60 @@
1
+ /**
2
+ * Lets the sample apps switch the color scheme at runtime.
3
+ *
4
+ * There is nothing KoliBri specific about it: `color-scheme` is an inherited CSS property and
5
+ * inheritance follows the flat tree, so setting it on `documentElement` reaches the page and every
6
+ * component's shadow root alike. The themes declare no `color-scheme` of their own precisely so
7
+ * that the application stays the single source of truth.
8
+ *
9
+ * `auto` removes the inline property again and lets the host app's own `:root { color-scheme: light
10
+ * dark }` apply, which is what hands the decision to the operating system. The
11
+ * `data-kol-color-scheme` attribute is set alongside it: it makes the state visible in the DOM and
12
+ * matches the selectors in `@public-ui/theme-default/color-scheme.css`. That stylesheet is not
13
+ * loaded here — the visual-test host has to stay theme agnostic and the presentation app offers six
14
+ * themes — so the inline property is what actually does the work.
15
+ *
16
+ * See `hooks/useColorScheme` for the URL parameter and the persistence.
17
+ */
18
+
19
+ const COLOR_SCHEME_PREFERENCES = ['auto', 'light', 'dark'] as const;
20
+ export type ColorSchemePreference = (typeof COLOR_SCHEME_PREFERENCES)[number];
21
+
22
+ const ROOT_ATTRIBUTE = 'data-kol-color-scheme';
23
+ const STORAGE_KEY = 'public-ui.sample.color-scheme';
24
+
25
+ export function isColorSchemePreference(value: unknown): value is ColorSchemePreference {
26
+ return typeof value === 'string' && (COLOR_SCHEME_PREFERENCES as readonly string[]).includes(value);
27
+ }
28
+
29
+ export function applyColorScheme(scheme: ColorSchemePreference): void {
30
+ const { documentElement } = document;
31
+ if (scheme === 'auto') {
32
+ documentElement.removeAttribute(ROOT_ATTRIBUTE);
33
+ documentElement.style.removeProperty('color-scheme');
34
+ } else {
35
+ documentElement.setAttribute(ROOT_ATTRIBUTE, scheme);
36
+ documentElement.style.setProperty('color-scheme', scheme);
37
+ }
38
+ }
39
+
40
+ /**
41
+ * Kept apart from `applyColorScheme`, because the scheme that ends up on the document is not always
42
+ * the one the user chose: a theme without a dark palette forces light, and that must not overwrite
43
+ * what they picked for the themes that do have one.
44
+ */
45
+ export function persistColorScheme(preference: ColorSchemePreference): void {
46
+ try {
47
+ localStorage.setItem(STORAGE_KEY, preference);
48
+ } catch {
49
+ // Storage can be unavailable (private mode, blocked cookies) – the choice just won't be remembered.
50
+ }
51
+ }
52
+
53
+ export function readStoredColorScheme(): ColorSchemePreference {
54
+ try {
55
+ const stored = localStorage.getItem(STORAGE_KEY);
56
+ return isColorSchemePreference(stored) ? stored : 'auto';
57
+ } catch {
58
+ return 'auto';
59
+ }
60
+ }
@@ -34,3 +34,9 @@ export const PUBLIC_THEMES: Theme[] = [
34
34
  key: 'desy-v11',
35
35
  },
36
36
  ];
37
+
38
+ /**
39
+ * Themes that ship a dark palette. Every other theme keeps its light colors whatever the color
40
+ * scheme says, which is what the Sidebar's hint tells the user.
41
+ */
42
+ export const DARK_CAPABLE_THEMES: string[] = ['default'];