@cratis/scene.blueprint.default 4.30.0 → 4.32.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.
Files changed (163) hide show
  1. package/ComponentName.ts +12 -0
  2. package/defaultBlueprint.ts +2 -1
  3. package/defaultBlueprintComponentMetadata.ts +2 -0
  4. package/defaultBlueprintComponents.ts +5 -0
  5. package/dist/cjs/ComponentName.js +4 -0
  6. package/dist/cjs/ComponentName.js.map +1 -1
  7. package/dist/cjs/defaultBlueprint.js +9 -5
  8. package/dist/cjs/defaultBlueprint.js.map +1 -1
  9. package/dist/cjs/defaultBlueprintComponentMetadata.js +8 -0
  10. package/dist/cjs/defaultBlueprintComponentMetadata.js.map +1 -1
  11. package/dist/cjs/defaultBlueprintComponents.js +13 -1
  12. package/dist/cjs/defaultBlueprintComponents.js.map +1 -1
  13. package/dist/cjs/foundation/ActionButton.js +33 -0
  14. package/dist/cjs/foundation/ActionButton.js.map +1 -0
  15. package/dist/cjs/foundation/ComponentState.js +42 -0
  16. package/dist/cjs/foundation/ComponentState.js.map +1 -0
  17. package/dist/cjs/foundation/EmptyState.js +31 -0
  18. package/dist/cjs/foundation/EmptyState.js.map +1 -0
  19. package/dist/cjs/foundation/FoundationIcon.js +58 -0
  20. package/dist/cjs/foundation/FoundationIcon.js.map +1 -0
  21. package/dist/cjs/foundation/FoundationStates.js +62 -0
  22. package/dist/cjs/foundation/FoundationStates.js.map +1 -0
  23. package/dist/cjs/foundation/PermissionRule.js +22 -0
  24. package/dist/cjs/foundation/PermissionRule.js.map +1 -0
  25. package/dist/cjs/foundation/StatCard.js +61 -0
  26. package/dist/cjs/foundation/StatCard.js.map +1 -0
  27. package/dist/cjs/foundation/StateFrame.js +200 -0
  28. package/dist/cjs/foundation/StateFrame.js.map +1 -0
  29. package/dist/cjs/foundation/StatusChip.js +64 -0
  30. package/dist/cjs/foundation/StatusChip.js.map +1 -0
  31. package/dist/cjs/foundation/TrendChip.js +93 -0
  32. package/dist/cjs/foundation/TrendChip.js.map +1 -0
  33. package/dist/cjs/foundation/formatting.js +85 -0
  34. package/dist/cjs/foundation/formatting.js.map +1 -0
  35. package/dist/cjs/foundation/foundationComponentMetadata.js +41 -0
  36. package/dist/cjs/foundation/foundationComponentMetadata.js.map +1 -0
  37. package/dist/cjs/foundation/foundationDescriptors.js +232 -0
  38. package/dist/cjs/foundation/foundationDescriptors.js.map +1 -0
  39. package/dist/cjs/foundation/foundationSamples.js +71 -0
  40. package/dist/cjs/foundation/foundationSamples.js.map +1 -0
  41. package/dist/cjs/foundation/foundationStrings.js +105 -0
  42. package/dist/cjs/foundation/foundationStrings.js.map +1 -0
  43. package/dist/cjs/index.js +52 -0
  44. package/dist/cjs/index.js.map +1 -1
  45. package/dist/cjs/themes/dark.js +9 -1
  46. package/dist/cjs/themes/dark.js.map +1 -1
  47. package/dist/cjs/themes/light.js +10 -2
  48. package/dist/cjs/themes/light.js.map +1 -1
  49. package/dist/esm/ComponentName.d.ts +5 -1
  50. package/dist/esm/ComponentName.d.ts.map +1 -1
  51. package/dist/esm/ComponentName.js +4 -0
  52. package/dist/esm/ComponentName.js.map +1 -1
  53. package/dist/esm/defaultBlueprint.d.ts.map +1 -1
  54. package/dist/esm/defaultBlueprint.js +9 -5
  55. package/dist/esm/defaultBlueprint.js.map +1 -1
  56. package/dist/esm/defaultBlueprintComponentMetadata.d.ts.map +1 -1
  57. package/dist/esm/defaultBlueprintComponentMetadata.js +8 -0
  58. package/dist/esm/defaultBlueprintComponentMetadata.js.map +1 -1
  59. package/dist/esm/defaultBlueprintComponents.d.ts.map +1 -1
  60. package/dist/esm/defaultBlueprintComponents.js +13 -1
  61. package/dist/esm/defaultBlueprintComponents.js.map +1 -1
  62. package/dist/esm/foundation/ActionButton.d.ts +13 -0
  63. package/dist/esm/foundation/ActionButton.d.ts.map +1 -0
  64. package/dist/esm/foundation/ActionButton.js +31 -0
  65. package/dist/esm/foundation/ActionButton.js.map +1 -0
  66. package/dist/esm/foundation/ComponentState.d.ts +12 -0
  67. package/dist/esm/foundation/ComponentState.d.ts.map +1 -0
  68. package/dist/esm/foundation/ComponentState.js +38 -0
  69. package/dist/esm/foundation/ComponentState.js.map +1 -0
  70. package/dist/esm/foundation/EmptyState.d.ts +3 -0
  71. package/dist/esm/foundation/EmptyState.d.ts.map +1 -0
  72. package/dist/esm/foundation/EmptyState.js +29 -0
  73. package/dist/esm/foundation/EmptyState.js.map +1 -0
  74. package/dist/esm/foundation/Foundation.stories.d.ts +21 -0
  75. package/dist/esm/foundation/Foundation.stories.d.ts.map +1 -0
  76. package/dist/esm/foundation/Foundation.stories.js +18 -0
  77. package/dist/esm/foundation/Foundation.stories.js.map +1 -0
  78. package/dist/esm/foundation/FoundationIcon.d.ts +7 -0
  79. package/dist/esm/foundation/FoundationIcon.d.ts.map +1 -0
  80. package/dist/esm/foundation/FoundationIcon.js +55 -0
  81. package/dist/esm/foundation/FoundationIcon.js.map +1 -0
  82. package/dist/esm/foundation/FoundationStates.d.ts +8 -0
  83. package/dist/esm/foundation/FoundationStates.d.ts.map +1 -0
  84. package/dist/esm/foundation/FoundationStates.js +60 -0
  85. package/dist/esm/foundation/FoundationStates.js.map +1 -0
  86. package/dist/esm/foundation/PermissionRule.d.ts +8 -0
  87. package/dist/esm/foundation/PermissionRule.d.ts.map +1 -0
  88. package/dist/esm/foundation/PermissionRule.js +19 -0
  89. package/dist/esm/foundation/PermissionRule.js.map +1 -0
  90. package/dist/esm/foundation/StatCard.d.ts +3 -0
  91. package/dist/esm/foundation/StatCard.d.ts.map +1 -0
  92. package/dist/esm/foundation/StatCard.js +59 -0
  93. package/dist/esm/foundation/StatCard.js.map +1 -0
  94. package/dist/esm/foundation/StateFrame.d.ts +15 -0
  95. package/dist/esm/foundation/StateFrame.d.ts.map +1 -0
  96. package/dist/esm/foundation/StateFrame.js +197 -0
  97. package/dist/esm/foundation/StateFrame.js.map +1 -0
  98. package/dist/esm/foundation/StatusChip.d.ts +13 -0
  99. package/dist/esm/foundation/StatusChip.d.ts.map +1 -0
  100. package/dist/esm/foundation/StatusChip.js +59 -0
  101. package/dist/esm/foundation/StatusChip.js.map +1 -0
  102. package/dist/esm/foundation/TrendChip.d.ts +18 -0
  103. package/dist/esm/foundation/TrendChip.d.ts.map +1 -0
  104. package/dist/esm/foundation/TrendChip.js +88 -0
  105. package/dist/esm/foundation/TrendChip.js.map +1 -0
  106. package/dist/esm/foundation/formatting.d.ts +13 -0
  107. package/dist/esm/foundation/formatting.d.ts.map +1 -0
  108. package/dist/esm/foundation/formatting.js +78 -0
  109. package/dist/esm/foundation/formatting.js.map +1 -0
  110. package/dist/esm/foundation/foundationComponentMetadata.d.ts +3 -0
  111. package/dist/esm/foundation/foundationComponentMetadata.d.ts.map +1 -0
  112. package/dist/esm/foundation/foundationComponentMetadata.js +39 -0
  113. package/dist/esm/foundation/foundationComponentMetadata.js.map +1 -0
  114. package/dist/esm/foundation/foundationDescriptors.d.ts +6 -0
  115. package/dist/esm/foundation/foundationDescriptors.d.ts.map +1 -0
  116. package/dist/esm/foundation/foundationDescriptors.js +227 -0
  117. package/dist/esm/foundation/foundationDescriptors.js.map +1 -0
  118. package/dist/esm/foundation/foundationSamples.d.ts +10 -0
  119. package/dist/esm/foundation/foundationSamples.d.ts.map +1 -0
  120. package/dist/esm/foundation/foundationSamples.js +68 -0
  121. package/dist/esm/foundation/foundationSamples.js.map +1 -0
  122. package/dist/esm/foundation/foundationStrings.d.ts +78 -0
  123. package/dist/esm/foundation/foundationStrings.d.ts.map +1 -0
  124. package/dist/esm/foundation/foundationStrings.js +101 -0
  125. package/dist/esm/foundation/foundationStrings.js.map +1 -0
  126. package/dist/esm/foundation/index.d.ts +16 -0
  127. package/dist/esm/foundation/index.d.ts.map +1 -0
  128. package/dist/esm/foundation/index.js +16 -0
  129. package/dist/esm/foundation/index.js.map +1 -0
  130. package/dist/esm/gallery/widgets.js +1 -1
  131. package/dist/esm/index.d.ts +1 -0
  132. package/dist/esm/index.d.ts.map +1 -1
  133. package/dist/esm/index.js +15 -0
  134. package/dist/esm/index.js.map +1 -1
  135. package/dist/esm/themes/dark.d.ts.map +1 -1
  136. package/dist/esm/themes/dark.js +9 -1
  137. package/dist/esm/themes/dark.js.map +1 -1
  138. package/dist/esm/themes/light.d.ts.map +1 -1
  139. package/dist/esm/themes/light.js +10 -2
  140. package/dist/esm/themes/light.js.map +1 -1
  141. package/dist/esm/tsconfig.tsbuildinfo +1 -1
  142. package/foundation/ActionButton.tsx +39 -0
  143. package/foundation/ComponentState.ts +53 -0
  144. package/foundation/EmptyState.tsx +22 -0
  145. package/foundation/Foundation.stories.tsx +30 -0
  146. package/foundation/FoundationIcon.tsx +49 -0
  147. package/foundation/FoundationStates.tsx +46 -0
  148. package/foundation/PermissionRule.ts +26 -0
  149. package/foundation/StatCard.tsx +44 -0
  150. package/foundation/StateFrame.tsx +126 -0
  151. package/foundation/StatusChip.tsx +51 -0
  152. package/foundation/TrendChip.tsx +78 -0
  153. package/foundation/formatting.ts +69 -0
  154. package/foundation/foundationComponentMetadata.ts +17 -0
  155. package/foundation/foundationDescriptors.ts +119 -0
  156. package/foundation/foundationSamples.ts +44 -0
  157. package/foundation/foundationStrings.ts +112 -0
  158. package/foundation/index.ts +18 -0
  159. package/index.ts +1 -0
  160. package/package.json +9 -8
  161. package/shell/layout.css +241 -0
  162. package/themes/dark.ts +8 -0
  163. package/themes/light.ts +9 -1
@@ -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 { ReactNode } from 'react';
5
+ import { PermissionRule } from './PermissionRule';
6
+
7
+ export interface ActionButtonProps {
8
+ /** The permission rule that decides whether the control works, refuses or is not rendered. */
9
+ rule?: PermissionRule;
10
+
11
+ /** Why the control is disabled, announced and shown as its tooltip. */
12
+ disabledReason: string;
13
+
14
+ /** What the control does when pressed. */
15
+ onClick?: (event: { stopPropagation(): void }) => void;
16
+
17
+ /** The emphasis of the button. */
18
+ emphasis?: 'primary' | 'secondary' | 'quiet';
19
+
20
+ children: ReactNode;
21
+ }
22
+
23
+ /**
24
+ * The one button every foundation component starts an action with, so the permission rule of spec Part B 1.5 is honoured in one place:
25
+ * hidden renders nothing; disabled stays focusable-by-description (`aria-disabled`) and carries the reason, rather than a bare
26
+ * greyed control nobody can explain.
27
+ */
28
+ export function ActionButton({ rule = PermissionRule.Enabled, disabledReason, onClick, emphasis = 'primary', children }: ActionButtonProps) {
29
+ if (rule === PermissionRule.Hidden) return undefined;
30
+
31
+ const disabled = rule === PermissionRule.Disabled;
32
+ return (
33
+ <button type='button' className='foundation-button' data-emphasis={emphasis} aria-disabled={disabled || undefined}
34
+ title={disabled ? disabledReason : undefined} onClick={disabled ? undefined : onClick}>
35
+ {children}
36
+ {disabled && <span className='foundation-visually-hidden'>{`. ${disabledReason}`}</span>}
37
+ </button>
38
+ );
39
+ }
@@ -0,0 +1,53 @@
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 { ExternalComponent } from '@cratis/scene.model';
5
+
6
+ /**
7
+ * The visual states every data-showing foundation component defines (spec Part B 1.4).
8
+ *
9
+ * A template is only worth choosing if it looks right in all of them: an application spends real time loading, empty, failing
10
+ * and not allowed, and a component that only draws its populated self has not been finished.
11
+ */
12
+ export enum ComponentState {
13
+ /** The data is there and drawn. */
14
+ Ready = 'ready',
15
+
16
+ /** The data has not answered yet: a skeleton in the shape of the content, never a spinner alone. */
17
+ Loading = 'loading',
18
+
19
+ /** The data answered with nothing: an explanation and the primary action. */
20
+ Empty = 'empty',
21
+
22
+ /** The data could not be loaded: a message and a retry. */
23
+ Error = 'error',
24
+
25
+ /** The user may not see the data: what is missing, and nothing of the data. */
26
+ NoPermission = 'noPermission',
27
+
28
+ /** Some regions loaded and some failed: what loaded, plus which parts did not. */
29
+ Partial = 'partial',
30
+ }
31
+
32
+ /** Every state, in the order a preview or a story lists them. */
33
+ export const componentStates: readonly ComponentState[] = Object.values(ComponentState);
34
+
35
+ /**
36
+ * Reads the state a component is in from its properties.
37
+ *
38
+ * An explicit `state` property wins, which is how a preview forces a state. Otherwise the state follows the properties a bound
39
+ * query already publishes - the same `loading` and `error` the preview states use for tables - plus `forbidden` and
40
+ * `failedRegions`, and falls to Empty when the component has nothing to show.
41
+ *
42
+ * @param element The element being rendered.
43
+ * @param hasData Whether the component has anything to draw when it is ready.
44
+ */
45
+ export function readComponentState(element: ExternalComponent, hasData: boolean): ComponentState {
46
+ const { state, forbidden, error, loading, failedRegions } = element.properties;
47
+ if (typeof state === 'string' && (componentStates as readonly string[]).includes(state)) return state as ComponentState;
48
+ if (forbidden === true) return ComponentState.NoPermission;
49
+ if (typeof error === 'string' && error.length > 0) return ComponentState.Error;
50
+ if (loading === true) return ComponentState.Loading;
51
+ if (Array.isArray(failedRegions) && failedRegions.length > 0) return ComponentState.Partial;
52
+ return hasData ? ComponentState.Ready : ComponentState.Empty;
53
+ }
@@ -0,0 +1,22 @@
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 { RegisteredComponentProps } from '@cratis/scene.react';
5
+ import { EmptyStateView } from './StateFrame';
6
+ import { readFoundationStrings } from './foundationStrings';
7
+ import { readLocale } from './formatting';
8
+
9
+ /**
10
+ * The empty state as a component of its own: an icon, a headline, a hint and one primary action, embeddable in any region.
11
+ *
12
+ * Every other foundation component shows this same view when it has no data, so a screen reads the same wherever nothing is
13
+ * found. Placed on its own it is what a template puts in a region that has nothing to say yet. The action starts whatever the
14
+ * element's behaviors say, and honours the `actionPermission` rule, so an empty list never offers a creation the user may not do.
15
+ */
16
+ export function EmptyState({ element, interactions }: RegisteredComponentProps) {
17
+ return (
18
+ <section className='foundation foundation-empty-state' data-scene-id={element.id} data-state='empty' lang={readLocale(element)}>
19
+ <EmptyStateView element={element} interactions={interactions} strings={readFoundationStrings(element)} />
20
+ </section>
21
+ );
22
+ }
@@ -0,0 +1,30 @@
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 type { Meta, StoryObj } from '@storybook/react';
5
+ import { ComponentName } from '../ComponentName';
6
+ import { FoundationStates } from './FoundationStates';
7
+
8
+ /**
9
+ * The foundation components (spec Part B 4), each in every standard state - ready, loading, empty, error, no permission and
10
+ * partial - in the light and the dark theme. Each component also has a right-to-left story.
11
+ */
12
+ const meta = {
13
+ title: 'Blueprint/Foundation components',
14
+ component: FoundationStates,
15
+ parameters: { layout: 'fullscreen' },
16
+ } satisfies Meta<typeof FoundationStates>;
17
+
18
+ export default meta;
19
+
20
+ type Story = StoryObj<typeof meta>;
21
+
22
+ export const EmptyStateLight: Story = { args: { component: ComponentName.EmptyState, theme: 'light' } };
23
+ export const EmptyStateDark: Story = { args: { component: ComponentName.EmptyState, theme: 'dark' } };
24
+ export const StatCardLight: Story = { args: { component: ComponentName.StatCard, theme: 'light' } };
25
+ export const StatCardDark: Story = { args: { component: ComponentName.StatCard, theme: 'dark' } };
26
+ export const StatCardRightToLeft: Story = { args: { component: ComponentName.StatCard, theme: 'light', rightToLeft: true, properties: { label: 'خط الأنابيب', locale: 'ar-EG' } } };
27
+ export const TrendChipLight: Story = { args: { component: ComponentName.TrendChip, theme: 'light' } };
28
+ export const TrendChipDark: Story = { args: { component: ComponentName.TrendChip, theme: 'dark' } };
29
+ export const StatusChipLight: Story = { args: { component: ComponentName.StatusChip, theme: 'light' } };
30
+ export const StatusChipDark: Story = { args: { component: ComponentName.StatusChip, theme: 'dark' } };
@@ -0,0 +1,49 @@
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
+ /** The icons the foundation components draw themselves, so none of them depends on an icon font being loaded. */
5
+ export type FoundationIconName =
6
+ | 'check' | 'close' | 'arrow-up' | 'arrow-down' | 'minus' | 'alert' | 'info' | 'lock' | 'clock' | 'inbox' | 'refresh'
7
+ | 'plus' | 'chevron-right' | 'chevron-down' | 'upload' | 'file' | 'user' | 'columns' | 'filter' | 'star' | 'circle';
8
+
9
+ const paths: Record<FoundationIconName, string> = {
10
+ check: 'M5 13l4 4L19 7',
11
+ close: 'M6 6l12 12M18 6L6 18',
12
+ 'arrow-up': 'M12 19V5M5 12l7-7 7 7',
13
+ 'arrow-down': 'M12 5v14M5 12l7 7 7-7',
14
+ minus: 'M5 12h14',
15
+ alert: 'M12 8v5M12 16.5v.5M10.3 3.9L2.5 17.5A2 2 0 004.2 20.5h15.6a2 2 0 001.7-3L13.7 3.9a2 2 0 00-3.4 0z',
16
+ info: 'M12 11v6M12 7.5v.5M12 21a9 9 0 100-18 9 9 0 000 18z',
17
+ lock: 'M7 11V8a5 5 0 0110 0v3M6 11h12a1 1 0 011 1v8a1 1 0 01-1 1H6a1 1 0 01-1-1v-8a1 1 0 011-1z',
18
+ clock: 'M12 7v5l3 2M12 21a9 9 0 100-18 9 9 0 000 18z',
19
+ inbox: 'M3 13l3-8h12l3 8M3 13v6a1 1 0 001 1h16a1 1 0 001-1v-6M3 13h5l1 3h6l1-3h5',
20
+ refresh: 'M20 11a8 8 0 10-2.3 6.3M20 4v7h-7',
21
+ plus: 'M12 5v14M5 12h14',
22
+ 'chevron-right': 'M9 5l7 7-7 7',
23
+ 'chevron-down': 'M5 9l7 7 7-7',
24
+ upload: 'M12 16V4M7 9l5-5 5 5M4 17v2a1 1 0 001 1h14a1 1 0 001-1v-2',
25
+ file: 'M14 3H7a1 1 0 00-1 1v16a1 1 0 001 1h10a1 1 0 001-1V7l-4-4zM14 3v4h4',
26
+ user: 'M12 12a4 4 0 100-8 4 4 0 000 8zM4 21a8 8 0 0116 0',
27
+ columns: 'M4 5h16v14H4zM9.5 5v14M14.5 5v14',
28
+ filter: 'M4 5h16l-6 8v6l-4-2v-4L4 5z',
29
+ star: 'M12 3l2.7 5.6 6.1.9-4.4 4.3 1 6.1L12 17l-5.4 2.9 1-6.1L3.2 9.5l6.1-.9L12 3z',
30
+ circle: 'M12 20a8 8 0 100-16 8 8 0 000 16z',
31
+ };
32
+
33
+ /** Whether a name is one of the icons drawn here. */
34
+ export function isFoundationIcon(name: string): name is FoundationIconName {
35
+ return name in paths;
36
+ }
37
+
38
+ /**
39
+ * One icon. It is decorative unless given a label: the meaning an icon carries is always repeated in text, so no status is
40
+ * conveyed by color or shape alone (WCAG 1.4.1).
41
+ */
42
+ export function FoundationIcon({ name, label }: { name: FoundationIconName; label?: string }) {
43
+ return (
44
+ <svg className='foundation-icon' viewBox='0 0 24 24' width='1em' height='1em' fill='none' stroke='currentColor' strokeWidth='2'
45
+ strokeLinecap='round' strokeLinejoin='round' aria-hidden={label ? undefined : true} role={label ? 'img' : undefined} aria-label={label}>
46
+ <path d={paths[name]} />
47
+ </svg>
48
+ );
49
+ }
@@ -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
+ import { componentRegistryKey, SceneThemeProvider } from '@cratis/scene.react';
5
+ import { defaultBlueprintComponents } from '../defaultBlueprintComponents';
6
+ import { defaultBlueprintName } from '../packageName';
7
+ import { darkTheme, lightTheme } from '../themes';
8
+ import { ComponentState, componentStates } from './ComponentState';
9
+ import { foundationElement, foundationSamples } from './foundationSamples';
10
+
11
+ export interface FoundationStatesProps {
12
+ /** The bare name of the foundation component. */
13
+ component: string;
14
+
15
+ /** The theme the states are drawn in. */
16
+ theme: 'light' | 'dark';
17
+
18
+ /** Properties to lay over the sample, to show a variant. */
19
+ properties?: Record<string, unknown>;
20
+
21
+ /** Whether the page reads right to left. */
22
+ rightToLeft?: boolean;
23
+ }
24
+
25
+ /**
26
+ * Every standard state of one foundation component side by side in one theme: the preview of spec Part B 6 ("all standard states
27
+ * implemented and previewable"), used by the stories and by the gallery.
28
+ */
29
+ export function FoundationStates({ component, theme, properties, rightToLeft }: FoundationStatesProps) {
30
+ const Component = defaultBlueprintComponents[componentRegistryKey(defaultBlueprintName, component)];
31
+ const sample = foundationSamples[component];
32
+ const states = sample?.stateful ? componentStates : [ComponentState.Ready];
33
+
34
+ return (
35
+ <SceneThemeProvider theme={theme === 'dark' ? darkTheme : lightTheme}>
36
+ <div dir={rightToLeft ? 'rtl' : 'ltr'} className='foundation-states' style={{ display: 'grid', gap: '1rem', padding: '1rem', gridTemplateColumns: 'repeat(auto-fill, minmax(18rem, 1fr))', background: 'var(--scene-surface-background)', color: 'var(--scene-text-color)' }}>
37
+ {states.map(state => (
38
+ <figure key={state} style={{ margin: 0 }}>
39
+ <figcaption style={{ fontSize: '0.75rem', textTransform: 'uppercase', marginBlockEnd: '0.5rem' }}>{state}</figcaption>
40
+ <Component element={foundationElement(component, { ...sample?.properties, ...properties }, state === ComponentState.Ready ? undefined : state)} slots={{}} />
41
+ </figure>
42
+ ))}
43
+ </div>
44
+ </SceneThemeProvider>
45
+ );
46
+ }
@@ -0,0 +1,26 @@
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 { ExternalComponent } from '@cratis/scene.model';
5
+
6
+ /**
7
+ * What an action control does when the user lacks the permission it needs (spec Part B 1.5): it is shown and works, shown and
8
+ * refuses, or not shown. A template never assumes a role, so every action declares one of these and the host's permission
9
+ * evaluation sets it.
10
+ */
11
+ export enum PermissionRule {
12
+ /** The control works. */
13
+ Enabled = 'enabled',
14
+
15
+ /** The control is shown but cannot be used, and says why. */
16
+ Disabled = 'disabled',
17
+
18
+ /** The control is not rendered. */
19
+ Hidden = 'hidden',
20
+ }
21
+
22
+ /** Reads a permission rule from a property, defaulting to Enabled for anything that is not a rule. */
23
+ export function readPermissionRule(element: ExternalComponent, property = 'permission'): PermissionRule {
24
+ const value = element.properties[property];
25
+ return value === PermissionRule.Disabled || value === PermissionRule.Hidden ? value : PermissionRule.Enabled;
26
+ }
@@ -0,0 +1,44 @@
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 { RegisteredComponentProps } from '@cratis/scene.react';
5
+ import { ComponentState, readComponentState } from './ComponentState';
6
+ import { FoundationFrame } from './StateFrame';
7
+ import { TrendChipView } from './TrendChip';
8
+ import { formatNumber, readLocale, readNumberFormat } from './formatting';
9
+ import { readFoundationStrings } from './foundationStrings';
10
+ import { readOptionalString, readString } from '../shell/elementProperties';
11
+
12
+ /**
13
+ * The `statCard` component: one key figure with its label, its trend and what the trend is compared to - the unit of a dashboard's
14
+ * `kpis` slot.
15
+ *
16
+ * The value is a bound number, never embedded in the component (a figure is data, not design); it is formatted for the locale as
17
+ * a number, currency, percent or compact. With no value the card is empty, and it shows all five standard states
18
+ * independently of its neighbours, so a dashboard whose one query failed still shows the other cards.
19
+ */
20
+ export function StatCard({ element, interactions }: RegisteredComponentProps) {
21
+ const value = element.properties.value;
22
+ const hasValue = typeof value === 'number';
23
+ const state = readComponentState(element, hasValue);
24
+ const locale = readLocale(element);
25
+ const label = readString(element, 'label');
26
+ const change = element.properties.change;
27
+ const currency = readOptionalString(element, 'currency');
28
+
29
+ return (
30
+ <FoundationFrame element={element} interactions={interactions}
31
+ name='stat-card' state={state} skeleton={[40, 70, 30]}>
32
+ {state !== ComponentState.Loading && (
33
+ <>
34
+ <h3 className='foundation-stat-label'>{label}</h3>
35
+ <p className='foundation-stat-value'>{hasValue ? formatNumber(value, locale, readNumberFormat(element), currency, typeof element.properties.fractionDigits === 'number' ? element.properties.fractionDigits : undefined) : ''}</p>
36
+ {typeof change === 'number' && (
37
+ <TrendChipView change={change} locale={locale} goodDirection={element.properties.goodDirection === 'down' ? 'down' : 'up'}
38
+ comparison={readOptionalString(element, 'comparison')} strings={readFoundationStrings(element)} />
39
+ )}
40
+ </>
41
+ )}
42
+ </FoundationFrame>
43
+ );
44
+ }
@@ -0,0 +1,126 @@
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 } from 'react';
5
+ import { ExternalComponent } from '@cratis/scene.model';
6
+ import { RegisteredComponentProps } from '@cratis/scene.react';
7
+ import { readOptionalString, readStrings } from '../shell/elementProperties';
8
+ import { ActionButton } from './ActionButton';
9
+ import { ComponentState } from './ComponentState';
10
+ import { FoundationIcon, FoundationIconName } from './FoundationIcon';
11
+ import { FoundationStrings, formatText, readFoundationStrings } from './foundationStrings';
12
+ import { readPermissionRule } from './PermissionRule';
13
+ import { readLocale } from './formatting';
14
+
15
+ export interface FoundationFrameProps extends Pick<RegisteredComponentProps, 'element' | 'interactions'> {
16
+ /** The component's own class, added after `foundation`. */
17
+ name: string;
18
+
19
+ /** The state the component is in; compute it with `readComponentState`. */
20
+ state: ComponentState;
21
+
22
+ /** The shape of the skeleton, as the width of each block in percent; the skeleton keeps the footprint of the content. */
23
+ skeleton?: number[];
24
+
25
+ /** What the component draws when it is ready or partial. */
26
+ children?: ReactNode;
27
+ }
28
+
29
+ /** The accessible name of a region, from the element's `label` property. */
30
+ function regionLabel(element: ExternalComponent): string | undefined {
31
+ return readOptionalString(element, 'label') ?? readOptionalString(element, 'title');
32
+ }
33
+
34
+ /**
35
+ * The root every foundation component renders, and the five standard states of spec Part B 1.4 in one place:
36
+ * a skeleton while loading (never a spinner alone), an explanation with the primary action when empty, a message with a
37
+ * retry on error, what is missing - and nothing of the data - when the user is not allowed, and the loaded part with a
38
+ * note naming what failed when only some regions did.
39
+ *
40
+ * The region carries an ARIA landmark role when it has a label, a `dir` is left to the document so right-to-left is
41
+ * inherited, and every piece of text comes from the strings table.
42
+ */
43
+ export function FoundationFrame({ element, interactions, name, state, skeleton = [60, 100, 80], children }: FoundationFrameProps) {
44
+ const strings = readFoundationStrings(element);
45
+ const label = regionLabel(element);
46
+ const density = element.properties.density === 'compact' ? 'compact' : 'comfortable';
47
+
48
+ return (
49
+ <section className={`foundation foundation-${name}`} data-scene-id={element.id} data-state={state} data-density={density}
50
+ lang={readLocale(element)} role={label ? 'region' : undefined} aria-label={label} aria-busy={state === ComponentState.Loading || undefined}>
51
+ {renderState(element, interactions, strings, state, skeleton, children)}
52
+ </section>
53
+ );
54
+ }
55
+
56
+ function renderState(element: ExternalComponent, interactions: RegisteredComponentProps['interactions'], strings: FoundationStrings, state: ComponentState, skeleton: number[], children: ReactNode) {
57
+ switch (state) {
58
+ case ComponentState.Loading:
59
+ return (
60
+ <div className='foundation-skeleton' role='status'>
61
+ <span className='foundation-visually-hidden'>{strings.loading}</span>
62
+ {skeleton.map((width, index) => <span key={index} className='foundation-skeleton-block' style={{ width: `${width}%` }} aria-hidden='true' />)}
63
+ </div>
64
+ );
65
+ case ComponentState.Empty:
66
+ return <EmptyStateView element={element} interactions={interactions} strings={strings} />;
67
+ case ComponentState.Error:
68
+ return (
69
+ <div className='foundation-message' data-tone='danger' role='alert'>
70
+ <FoundationIcon name='alert' />
71
+ <div className='foundation-message-text'>
72
+ <strong>{strings.errorHeadline}</strong>
73
+ <span>{typeof element.properties.error === 'string' && element.properties.error.length > 0 ? element.properties.error : strings.errorHint}</span>
74
+ </div>
75
+ <ActionButton emphasis='secondary' disabledReason={strings.disabledNoPermission} onClick={interactions?.onClick}>{strings.retry}</ActionButton>
76
+ </div>
77
+ );
78
+ case ComponentState.NoPermission: {
79
+ const subject = readOptionalString(element, 'permissionSubject') ?? strings.noPermissionSubject;
80
+ return (
81
+ <div className='foundation-message' data-tone='neutral' role='status'>
82
+ <FoundationIcon name='lock' />
83
+ <div className='foundation-message-text'>
84
+ <strong>{strings.noPermissionHeadline}</strong>
85
+ <span>{formatText(strings.noPermissionHint, { subject })}</span>
86
+ </div>
87
+ </div>
88
+ );
89
+ }
90
+
91
+ case ComponentState.Partial:
92
+ return (
93
+ <>
94
+ <div className='foundation-message' data-tone='warning' role='status'>
95
+ <FoundationIcon name='alert' />
96
+ <div className='foundation-message-text'>
97
+ <strong>{strings.partialHeadline}</strong>
98
+ {readStrings(element, 'failedRegions').length > 0 && <span>{formatText(strings.partialHint, { regions: readStrings(element, 'failedRegions').join(', ') })}</span>}
99
+ </div>
100
+ <ActionButton emphasis='secondary' disabledReason={strings.disabledNoPermission} onClick={interactions?.onClick}>{strings.retry}</ActionButton>
101
+ </div>
102
+ {children}
103
+ </>
104
+ );
105
+ default:
106
+ return <>{children}</>;
107
+ }
108
+ }
109
+
110
+ /** What an empty region says: an icon, a headline, a hint and the primary action, with the action's permission rule applied. */
111
+ export function EmptyStateView({ element, interactions, strings }: Pick<FoundationFrameProps, 'element' | 'interactions'> & { strings: FoundationStrings }) {
112
+ const icon = (readOptionalString(element, 'emptyIcon') ?? readOptionalString(element, 'icon')) as FoundationIconName | undefined;
113
+ const actionLabel = readOptionalString(element, 'emptyActionLabel') ?? readOptionalString(element, 'actionLabel');
114
+
115
+ return (
116
+ <div className='foundation-empty' role='status'>
117
+ <span className='foundation-empty-icon'><FoundationIcon name={icon ?? 'inbox'} /></span>
118
+ <strong className='foundation-empty-headline'>{readOptionalString(element, 'emptyHeadline') ?? readOptionalString(element, 'headline') ?? strings.emptyHeadline}</strong>
119
+ <span className='foundation-empty-hint'>{readOptionalString(element, 'emptyHint') ?? readOptionalString(element, 'hint') ?? strings.emptyHint}</span>
120
+ {actionLabel && (
121
+ <ActionButton rule={readPermissionRule(element, 'actionPermission')} disabledReason={strings.disabledNoPermission} onClick={interactions?.onClick}>{actionLabel}</ActionButton>
122
+ )}
123
+ </div>
124
+ );
125
+ }
126
+
@@ -0,0 +1,51 @@
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 { RegisteredComponentProps } from '@cratis/scene.react';
5
+ import { readFoundationStrings } from './foundationStrings';
6
+ import { FoundationIcon, FoundationIconName } from './FoundationIcon';
7
+ import { readOptionalString, readString } from '../shell/elementProperties';
8
+
9
+ /** The semantic tone of a status. */
10
+ export enum StatusTone {
11
+ Success = 'success',
12
+ Warning = 'warning',
13
+ Danger = 'danger',
14
+ Info = 'info',
15
+ Neutral = 'neutral',
16
+ }
17
+
18
+ /** The icon each tone gets, so a status is told by shape as well as color (WCAG 1.4.1). */
19
+ export const statusToneIcons: Record<StatusTone, FoundationIconName> = {
20
+ [StatusTone.Success]: 'check',
21
+ [StatusTone.Warning]: 'alert',
22
+ [StatusTone.Danger]: 'close',
23
+ [StatusTone.Info]: 'info',
24
+ [StatusTone.Neutral]: 'circle',
25
+ };
26
+
27
+ /** Reads a tone, defaulting to Neutral. */
28
+ export function readStatusTone(value: unknown): StatusTone {
29
+ return (Object.values(StatusTone) as string[]).includes(value as string) ? (value as StatusTone) : StatusTone.Neutral;
30
+ }
31
+
32
+ /**
33
+ * The `statusChip` component: a status as a semantic color, an icon and its text.
34
+ *
35
+ * The text is always shown - the status of a record is never only a colored dot - and the tone picks the icon and the color from
36
+ * the theme's status tokens. A status the application names ("Awaiting approval") is its own `status` text; `tone` maps it onto
37
+ * one of the five tones.
38
+ */
39
+ export function StatusChip({ element }: RegisteredComponentProps) {
40
+ const tone = readStatusTone(element.properties.tone);
41
+ const strings = readFoundationStrings(element);
42
+ const fallback = { success: strings.statusSuccess, warning: strings.statusWarning, danger: strings.statusDanger, info: strings.statusInfo, neutral: strings.statusNeutral }[tone];
43
+ const text = readString(element, 'status', readOptionalString(element, 'label') ?? fallback);
44
+
45
+ return (
46
+ <span className='foundation foundation-chip foundation-status-chip' data-scene-id={element.id} data-tone={tone}>
47
+ <FoundationIcon name={statusToneIcons[tone]} />
48
+ <span>{text}</span>
49
+ </span>
50
+ );
51
+ }
@@ -0,0 +1,78 @@
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 { RegisteredComponentProps } from '@cratis/scene.react';
5
+ import { FoundationIcon } from './FoundationIcon';
6
+ import { formatNumber, NumberFormat, readLocale } from './formatting';
7
+ import { formatText, readFoundationStrings } from './foundationStrings';
8
+ import { readOptionalString } from '../shell/elementProperties';
9
+
10
+ /** The direction a measure moved. */
11
+ export type TrendDirection = 'up' | 'down' | 'flat';
12
+
13
+ /** Whether a movement is good, bad or neither, which is independent of whether the number rose: falling response time is good. */
14
+ export type TrendOutcome = 'good' | 'bad' | 'neutral';
15
+
16
+ /** The direction a change moved in. */
17
+ export function trendDirection(change: number): TrendDirection {
18
+ return change > 0 ? 'up' : change < 0 ? 'down' : 'flat';
19
+ }
20
+
21
+ /** Whether a movement is good for a measure, given which direction is the good one. */
22
+ export function trendOutcome(direction: TrendDirection, goodDirection: 'up' | 'down'): TrendOutcome {
23
+ return direction === 'flat' ? 'neutral' : direction === goodDirection ? 'good' : 'bad';
24
+ }
25
+
26
+ /** The tone each outcome is drawn in. */
27
+ const tones: Record<TrendOutcome, string> = { good: 'success', bad: 'danger', neutral: 'neutral' };
28
+
29
+ /** The props of a trend chip drawn directly, as the stat card does. */
30
+ export interface TrendChipViewProps {
31
+ change: number;
32
+ locale: string;
33
+ goodDirection?: 'up' | 'down';
34
+ comparison?: string;
35
+ format?: NumberFormat;
36
+ strings: ReturnType<typeof readFoundationStrings>;
37
+ }
38
+
39
+ /**
40
+ * A change, drawn as an arrow, the change and what it is compared to.
41
+ *
42
+ * Color says whether the movement is good, but never alone: the arrow gives direction and a visually hidden sentence
43
+ * says all of it ("up 12.5% compared to last month"), so the chip reads the same without color.
44
+ */
45
+ export function TrendChipView({ change, locale, goodDirection = 'up', comparison, format = NumberFormat.Percent, strings }: TrendChipViewProps) {
46
+ const direction = trendDirection(change);
47
+ const outcome = trendOutcome(direction, goodDirection);
48
+ const magnitude = formatNumber(Math.abs(change), locale, format);
49
+ const spoken = direction === 'flat' ? strings.trendFlat : formatText(direction === 'up' ? strings.trendUp : strings.trendDown, { value: magnitude });
50
+
51
+ return (
52
+ <span className='foundation-chip foundation-trend' data-tone={tones[outcome]} data-direction={direction}>
53
+ <FoundationIcon name={direction === 'up' ? 'arrow-up' : direction === 'down' ? 'arrow-down' : 'minus'} />
54
+ <span aria-hidden='true'>{direction === 'flat' ? magnitude : magnitude}</span>
55
+ <span className='foundation-visually-hidden'>{spoken}{comparison ? ` ${formatText(strings.trendComparison, { period: comparison })}` : ''}</span>
56
+ {comparison && <span className='foundation-trend-comparison' aria-hidden='true'>{formatText(strings.trendComparison, { period: comparison })}</span>}
57
+ </span>
58
+ );
59
+ }
60
+
61
+ /**
62
+ * The `trendChip` component: a change with its direction, whether that is good, and what it is compared to.
63
+ *
64
+ * `change` is the movement as a number (a percentage by default), `goodDirection` says which way is good, and `comparison` names
65
+ * the period. Without a number the chip is empty, and then it draws nothing rather than an invented zero.
66
+ */
67
+ export function TrendChip({ element }: RegisteredComponentProps) {
68
+ const change = element.properties.change;
69
+ if (typeof change !== 'number') return undefined;
70
+
71
+ return (
72
+ <span className='foundation foundation-trend-chip' data-scene-id={element.id}>
73
+ <TrendChipView change={change} locale={readLocale(element)} goodDirection={element.properties.goodDirection === 'down' ? 'down' : 'up'}
74
+ comparison={readOptionalString(element, 'comparison')} strings={readFoundationStrings(element)}
75
+ {...(typeof element.properties.format === 'string' && (Object.values(NumberFormat) as string[]).includes(element.properties.format) ? { format: element.properties.format as NumberFormat } : {})} />
76
+ </span>
77
+ );
78
+ }
@@ -0,0 +1,69 @@
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 { ExternalComponent } from '@cratis/scene.model';
5
+
6
+ /** How a number is presented. */
7
+ export enum NumberFormat {
8
+ Number = 'number',
9
+ Currency = 'currency',
10
+ Percent = 'percent',
11
+ Compact = 'compact',
12
+ }
13
+
14
+ /**
15
+ * The locale a component formats dates, numbers and currency in: the element's `locale`, else the document language, else the
16
+ * runtime's. Formatting never goes through a fixed locale, so the same screen reads correctly in every one.
17
+ */
18
+ export function readLocale(element: ExternalComponent): string {
19
+ const supplied = element.properties.locale;
20
+ if (typeof supplied === 'string' && supplied.length > 0) return supplied;
21
+ if (typeof document !== 'undefined' && document.documentElement.lang) return document.documentElement.lang;
22
+ return typeof navigator !== 'undefined' && navigator.language ? navigator.language : 'en';
23
+ }
24
+
25
+ /** Whether the locale reads right to left, so a component can mirror what direction-sensitive CSS cannot. */
26
+ export function isRightToLeft(locale: string): boolean {
27
+ try {
28
+ const info = new Intl.Locale(locale) as Intl.Locale & { getTextInfo?: () => { direction: string }; textInfo?: { direction: string } };
29
+ const direction = info.getTextInfo?.().direction ?? info.textInfo?.direction;
30
+ return direction === 'rtl';
31
+ } catch {
32
+ return false;
33
+ }
34
+ }
35
+
36
+ /** Formats a number for a locale, as a plain number, currency, percent (the value is the percentage, 12.5 is 12.5%) or compact. */
37
+ export function formatNumber(value: number, locale: string, format: NumberFormat = NumberFormat.Number, currency = 'USD', maximumFractionDigits?: number): string {
38
+ try {
39
+ switch (format) {
40
+ case NumberFormat.Currency:
41
+ return new Intl.NumberFormat(locale, { style: 'currency', currency, ...(maximumFractionDigits === undefined ? {} : { maximumFractionDigits }) }).format(value);
42
+ case NumberFormat.Percent:
43
+ return new Intl.NumberFormat(locale, { style: 'percent', maximumFractionDigits: maximumFractionDigits ?? 1 }).format(value / 100);
44
+ case NumberFormat.Compact:
45
+ return new Intl.NumberFormat(locale, { notation: 'compact', maximumFractionDigits: maximumFractionDigits ?? 1 }).format(value);
46
+ default:
47
+ return new Intl.NumberFormat(locale, { maximumFractionDigits: maximumFractionDigits ?? 2 }).format(value);
48
+ }
49
+ } catch {
50
+ return String(value);
51
+ }
52
+ }
53
+
54
+ /** Reads a number format name, defaulting to a plain number. */
55
+ export function readNumberFormat(element: ExternalComponent, property = 'format'): NumberFormat {
56
+ const value = element.properties[property];
57
+ return (Object.values(NumberFormat) as string[]).includes(value as string) ? (value as NumberFormat) : NumberFormat.Number;
58
+ }
59
+
60
+ /** Formats a date or an ISO 8601 date-time for a locale; a value that is not a date is returned as it was written. */
61
+ export function formatDate(value: unknown, locale: string, withTime = false): string {
62
+ const date = value instanceof Date ? value : typeof value === 'string' || typeof value === 'number' ? new Date(value) : undefined;
63
+ if (!date || Number.isNaN(date.getTime())) return typeof value === 'string' ? value : '';
64
+ try {
65
+ return new Intl.DateTimeFormat(locale, withTime ? { dateStyle: 'medium', timeStyle: 'short' } : { dateStyle: 'medium' }).format(date);
66
+ } catch {
67
+ return date.toISOString();
68
+ }
69
+ }