@cratis/scene.react 2.0.0 → 3.0.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 +29 -2
- package/dist/cjs/renderer/createReactRenderer.js +21 -6
- package/dist/cjs/renderer/createReactRenderer.js.map +1 -1
- package/dist/cjs/renderer/panelLayout.js +108 -0
- package/dist/cjs/renderer/panelLayout.js.map +1 -0
- package/dist/esm/for_panelLayout/when_arranging_a_panel.d.ts +2 -0
- package/dist/esm/for_panelLayout/when_arranging_a_panel.d.ts.map +1 -0
- package/dist/esm/for_panelLayout/when_arranging_a_panel.js +91 -0
- package/dist/esm/for_panelLayout/when_arranging_a_panel.js.map +1 -0
- package/dist/esm/renderer/createReactRenderer.d.ts.map +1 -1
- package/dist/esm/renderer/createReactRenderer.js +21 -6
- package/dist/esm/renderer/createReactRenderer.js.map +1 -1
- package/dist/esm/renderer/panelLayout.d.ts +5 -0
- package/dist/esm/renderer/panelLayout.d.ts.map +1 -0
- package/dist/esm/renderer/panelLayout.js +105 -0
- package/dist/esm/renderer/panelLayout.js.map +1 -0
- package/dist/esm/tsconfig.tsbuildinfo +1 -1
- package/for_panelLayout/when_arranging_a_panel.ts +112 -0
- package/package.json +15 -5
- package/renderer/createReactRenderer.tsx +21 -5
- package/renderer/panelLayout.ts +109 -0
|
@@ -0,0 +1,112 @@
|
|
|
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 { ColumnDefinition, Dock, DockPanel, Grid, GridUnitType, HorizontalAlignment, Orientation, RowDefinition, SceneElement, StackPanel, VerticalAlignment, Visibility, WrapPanel } from '@cratis/scene.model';
|
|
5
|
+
import { childStyle, panelStyle } from '../renderer/panelLayout';
|
|
6
|
+
|
|
7
|
+
const base = {
|
|
8
|
+
id: 'panel',
|
|
9
|
+
properties: {},
|
|
10
|
+
visibility: Visibility.Visible,
|
|
11
|
+
isEnabled: true,
|
|
12
|
+
opacity: 1,
|
|
13
|
+
zIndex: 0,
|
|
14
|
+
size: {},
|
|
15
|
+
name: 'panel',
|
|
16
|
+
minimumSize: {},
|
|
17
|
+
maximumSize: {},
|
|
18
|
+
margin: { left: 0, top: 0, right: 0, bottom: 0 },
|
|
19
|
+
horizontalAlignment: HorizontalAlignment.Stretch,
|
|
20
|
+
verticalAlignment: VerticalAlignment.Stretch,
|
|
21
|
+
children: [] as SceneElement[],
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const child = (properties: Record<string, unknown> = {}): SceneElement => ({ id: 'child', properties });
|
|
25
|
+
|
|
26
|
+
const star = (value: number): RowDefinition['height'] => ({ value, unitType: GridUnitType.Star });
|
|
27
|
+
const row = (over: Partial<RowDefinition> = {}): RowDefinition => ({ height: star(1), minimumHeight: 0, maximumHeight: Number.POSITIVE_INFINITY, ...over });
|
|
28
|
+
const column = (over: Partial<ColumnDefinition> = {}): ColumnDefinition => ({ width: star(1), minimumWidth: 0, maximumWidth: Number.POSITIVE_INFINITY, ...over });
|
|
29
|
+
|
|
30
|
+
describe('when arranging a panel', () => {
|
|
31
|
+
it('should lay a stack panel out along its orientation', () => {
|
|
32
|
+
const panel: StackPanel = { ...base, orientation: Orientation.Horizontal, spacing: 8 };
|
|
33
|
+
panelStyle(panel)!.flexDirection!.should.equal('row');
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('should space a stack panel by its spacing', () => {
|
|
37
|
+
const panel: StackPanel = { ...base, orientation: Orientation.Vertical, spacing: 12 };
|
|
38
|
+
panelStyle(panel)!.gap!.should.equal('12px');
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('should wrap a wrap panel', () => {
|
|
42
|
+
const panel: WrapPanel = { ...base, orientation: Orientation.Horizontal };
|
|
43
|
+
panelStyle(panel)!.flexWrap!.should.equal('wrap');
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('should turn star tracks into fractions', () => {
|
|
47
|
+
const panel: Grid = { ...base, rows: [row(), row({ height: star(2) })], columns: [] };
|
|
48
|
+
panelStyle(panel)!.gridTemplateRows!.should.equal('1fr 2fr');
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('should turn an absolute track into pixels', () => {
|
|
52
|
+
const panel: Grid = { ...base, rows: [], columns: [column({ width: { value: 240, unitType: GridUnitType.Absolute } })] };
|
|
53
|
+
panelStyle(panel)!.gridTemplateColumns!.should.equal('240px');
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('should turn a content-sized track into auto', () => {
|
|
57
|
+
const panel: Grid = { ...base, rows: [], columns: [column({ width: { value: 0, unitType: GridUnitType.Auto } })] };
|
|
58
|
+
panelStyle(panel)!.gridTemplateColumns!.should.equal('auto');
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('should bound a track that states a minimum', () => {
|
|
62
|
+
const panel: Grid = { ...base, rows: [row({ minimumHeight: 40 })], columns: [] };
|
|
63
|
+
panelStyle(panel)!.gridTemplateRows!.should.equal('minmax(40px, 1fr)');
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('should leave a plain panel unstyled', () => (panelStyle({ ...base }) === undefined).should.be.true);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
describe('when placing a child in a panel', () => {
|
|
70
|
+
it('should place a grid child in the cell it names, one-based for CSS', () => {
|
|
71
|
+
const panel: Grid = { ...base, rows: [row()], columns: [column()] };
|
|
72
|
+
childStyle(panel, child({ 'Grid.Row': 1, 'Grid.Column': 2 }), 0)!.gridRow!.should.equal('2 / span 1');
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('should span a grid child that asks for it', () => {
|
|
76
|
+
const panel: Grid = { ...base, rows: [row()], columns: [column()] };
|
|
77
|
+
childStyle(panel, child({ 'Grid.Column': 0, 'Grid.ColumnSpan': 3 }), 0)!.gridColumn!.should.equal('1 / span 3');
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('should leave a grid child that names no cell to the implicit flow', () => {
|
|
81
|
+
const panel: Grid = { ...base, rows: [row()], columns: [column()] };
|
|
82
|
+
(childStyle(panel, child(), 0) === undefined).should.be.true;
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it('should let a dock panel last child fill what is left', () => {
|
|
86
|
+
const kid = child();
|
|
87
|
+
const panel: DockPanel = { ...base, lastChildFill: true, children: [kid] };
|
|
88
|
+
childStyle(panel, kid, 0)!.flex!.should.equal('1 1 auto');
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('should keep a docked child at its natural size', () => {
|
|
92
|
+
const first = child({ Dock: Dock.Top });
|
|
93
|
+
const panel: DockPanel = { ...base, lastChildFill: true, children: [first, child()] };
|
|
94
|
+
childStyle(panel, first, 0)!.flex!.should.equal('0 0 auto');
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('should not let the last child fill when the panel says not to', () => {
|
|
98
|
+
const kid = child();
|
|
99
|
+
const panel: DockPanel = { ...base, lastChildFill: false, children: [kid] };
|
|
100
|
+
childStyle(panel, kid, 0)!.flex!.should.equal('0 0 auto');
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it('should size wrap panel children when the panel sizes them', () => {
|
|
104
|
+
const panel: WrapPanel = { ...base, orientation: Orientation.Horizontal, itemWidth: 120 };
|
|
105
|
+
childStyle(panel, child(), 0)!.width!.should.equal(120);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it('should leave wrap panel children their own size when the panel sizes neither axis', () => {
|
|
109
|
+
const panel: WrapPanel = { ...base, orientation: Orientation.Horizontal };
|
|
110
|
+
(childStyle(panel, child(), 0) === undefined).should.be.true;
|
|
111
|
+
});
|
|
112
|
+
});
|
package/package.json
CHANGED
|
@@ -1,7 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cratis/scene.react",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "The React renderer implementing Scene.Engine's Renderer contract against real React/DOM.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"cratis",
|
|
7
|
+
"scene",
|
|
8
|
+
"react",
|
|
9
|
+
"ui",
|
|
10
|
+
"renderer",
|
|
11
|
+
"event-modeling",
|
|
12
|
+
"cqrs",
|
|
13
|
+
"low-code"
|
|
14
|
+
],
|
|
5
15
|
"author": "Cratis",
|
|
6
16
|
"license": "MIT",
|
|
7
17
|
"repository": {
|
|
@@ -40,12 +50,12 @@
|
|
|
40
50
|
"build-storybook": "storybook build"
|
|
41
51
|
},
|
|
42
52
|
"dependencies": {
|
|
43
|
-
"@cratis/scene.engine": "
|
|
44
|
-
"@cratis/scene.model": "
|
|
53
|
+
"@cratis/scene.engine": "3.0.0",
|
|
54
|
+
"@cratis/scene.model": "3.0.0"
|
|
45
55
|
},
|
|
46
56
|
"devDependencies": {
|
|
47
|
-
"@cratis/scene.engine": "
|
|
48
|
-
"@cratis/scene.model": "
|
|
57
|
+
"@cratis/scene.engine": "3.0.0",
|
|
58
|
+
"@cratis/scene.model": "3.0.0",
|
|
49
59
|
"@storybook/addon-links": "^10.4.1",
|
|
50
60
|
"@storybook/react": "^10.4.1",
|
|
51
61
|
"@storybook/react-vite": "^10.4.1",
|
|
@@ -5,13 +5,16 @@ import { ReactNode, createElement } from 'react';
|
|
|
5
5
|
import { Renderer } from '@cratis/scene.engine';
|
|
6
6
|
import { ContentControl, ExternalComponent, ItemsControl, Panel } from '@cratis/scene.model';
|
|
7
7
|
import { ComponentRegistry } from './ComponentRegistry';
|
|
8
|
+
import { childStyle, panelStyle } from './panelLayout';
|
|
8
9
|
import { UnresolvedComponent } from './UnresolvedComponent';
|
|
9
10
|
|
|
10
11
|
/**
|
|
11
|
-
* Creates a {@link Renderer} that turns a Scene element tree into React elements.
|
|
12
|
-
*
|
|
13
|
-
* `
|
|
14
|
-
*
|
|
12
|
+
* Creates a {@link Renderer} that turns a Scene element tree into React elements.
|
|
13
|
+
*
|
|
14
|
+
* `ItemsControl` and `ContentControl` render as plain wrapping `div`s. A `Panel` renders as the
|
|
15
|
+
* arrangement it declares - a grid, a stack, a wrap or a dock - because that is what those panels are
|
|
16
|
+
* for; see `panelLayout` for the mapping. Arranging a *layout's slots* by `flow`/`freeform` is a
|
|
17
|
+
* different thing and stays Scene#4's, layered on top of this renderer rather than folded into it.
|
|
15
18
|
*/
|
|
16
19
|
export function createReactRenderer(registry: ComponentRegistry): Renderer<ReactNode> {
|
|
17
20
|
return {
|
|
@@ -29,7 +32,20 @@ export function createReactRenderer(registry: ComponentRegistry): Renderer<React
|
|
|
29
32
|
},
|
|
30
33
|
|
|
31
34
|
renderPanel(element: Panel, children: ReactNode[]): ReactNode {
|
|
32
|
-
|
|
35
|
+
// The rendered children arrive in the same order as `element.children`, so a panel that places
|
|
36
|
+
// its children individually - a grid cell, a docked edge - can wrap each one in the box that
|
|
37
|
+
// places it without needing to re-render anything.
|
|
38
|
+
const placed = children.map((child, index) => {
|
|
39
|
+
const style = childStyle(element, element.children[index], index);
|
|
40
|
+
return style
|
|
41
|
+
? createElement('div', { key: `${element.id}-${index}`, 'data-scene-placement': index, style }, child)
|
|
42
|
+
: child;
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
return createElement(
|
|
46
|
+
'div',
|
|
47
|
+
{ key: element.id, 'data-scene-id': element.id, 'data-scene-kind': 'Panel', style: panelStyle(element) },
|
|
48
|
+
placed);
|
|
33
49
|
},
|
|
34
50
|
};
|
|
35
51
|
}
|
|
@@ -0,0 +1,109 @@
|
|
|
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 { CSSProperties } from 'react';
|
|
5
|
+
import { isDockPanel, isGrid, isStackPanel, isWrapPanel } from '@cratis/scene.engine';
|
|
6
|
+
import { Dock, GridUnitType, Orientation } from '@cratis/scene.model';
|
|
7
|
+
import type { ColumnDefinition, GridLength, Panel, RowDefinition, SceneElement } from '@cratis/scene.model';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Turns a panel into the CSS that arranges it, and a child into the CSS that places it inside one.
|
|
11
|
+
*
|
|
12
|
+
* The panels say what an arrangement *is*; this says what it looks like on the web. A second renderer -
|
|
13
|
+
* native, desktop - answers the same question with its own platform's layout primitives, which is why
|
|
14
|
+
* this lives in `Scene.React` rather than in the model or the engine.
|
|
15
|
+
*
|
|
16
|
+
* Where a panel needs to say something about an individual child - which grid cell, which edge - it does
|
|
17
|
+
* so through that child's own `properties` bag, because the model has no attached properties of its own.
|
|
18
|
+
* The keys are the ones the model's documentation names: `Grid.Row`, `Grid.Column`, `Grid.RowSpan`,
|
|
19
|
+
* `Grid.ColumnSpan` and `Dock`.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
const track = (length: GridLength): string => {
|
|
23
|
+
switch (length.unitType) {
|
|
24
|
+
case GridUnitType.Auto: return 'auto';
|
|
25
|
+
case GridUnitType.Absolute: return `${length.value}px`;
|
|
26
|
+
case GridUnitType.Star: return `${length.value}fr`;
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const bound = (minimum: number, maximum: number, size: string): string =>
|
|
31
|
+
minimum === 0 && !Number.isFinite(maximum)
|
|
32
|
+
? size
|
|
33
|
+
: `minmax(${minimum}px, ${Number.isFinite(maximum) ? `${maximum}px` : size})`;
|
|
34
|
+
|
|
35
|
+
const rowTrack = (row: RowDefinition): string => bound(row.minimumHeight, row.maximumHeight, track(row.height));
|
|
36
|
+
const columnTrack = (column: ColumnDefinition): string => bound(column.minimumWidth, column.maximumWidth, track(column.width));
|
|
37
|
+
|
|
38
|
+
const number = (value: unknown): number | undefined => (typeof value === 'number' ? value : undefined);
|
|
39
|
+
|
|
40
|
+
/** The CSS that arranges a panel's own children. */
|
|
41
|
+
export function panelStyle(panel: Panel): CSSProperties | undefined {
|
|
42
|
+
if (isGrid(panel)) {
|
|
43
|
+
return {
|
|
44
|
+
display: 'grid',
|
|
45
|
+
gridTemplateRows: panel.rows.map(rowTrack).join(' ') || undefined,
|
|
46
|
+
gridTemplateColumns: panel.columns.map(columnTrack).join(' ') || undefined,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (isDockPanel(panel)) {
|
|
51
|
+
// Docking has no single CSS primitive. A column of rows with a middle that grows is the shape a
|
|
52
|
+
// dock panel actually produces, and `lastChildFill` decides whether the final child is that
|
|
53
|
+
// middle or just another docked edge.
|
|
54
|
+
return { display: 'flex', flexDirection: 'column' };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (isStackPanel(panel)) {
|
|
58
|
+
return {
|
|
59
|
+
display: 'flex',
|
|
60
|
+
flexDirection: panel.orientation === Orientation.Horizontal ? 'row' : 'column',
|
|
61
|
+
gap: panel.spacing ? `${panel.spacing}px` : undefined,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (isWrapPanel(panel)) {
|
|
66
|
+
return {
|
|
67
|
+
display: 'flex',
|
|
68
|
+
flexWrap: 'wrap',
|
|
69
|
+
flexDirection: panel.orientation === Orientation.Horizontal ? 'row' : 'column',
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return undefined;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** The CSS that places one child within its panel, or `undefined` when the panel places children implicitly. */
|
|
77
|
+
export function childStyle(panel: Panel, child: SceneElement, index: number): CSSProperties | undefined {
|
|
78
|
+
const properties = child.properties ?? {};
|
|
79
|
+
|
|
80
|
+
if (isGrid(panel)) {
|
|
81
|
+
const row = number(properties['Grid.Row']);
|
|
82
|
+
const column = number(properties['Grid.Column']);
|
|
83
|
+
const rowSpan = number(properties['Grid.RowSpan']) ?? 1;
|
|
84
|
+
const columnSpan = number(properties['Grid.ColumnSpan']) ?? 1;
|
|
85
|
+
if (row === undefined && column === undefined) return undefined;
|
|
86
|
+
return {
|
|
87
|
+
gridRow: row === undefined ? undefined : `${row + 1} / span ${rowSpan}`,
|
|
88
|
+
gridColumn: column === undefined ? undefined : `${column + 1} / span ${columnSpan}`,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (isDockPanel(panel)) {
|
|
93
|
+
const dock = properties['Dock'] as Dock | undefined;
|
|
94
|
+
const isFill = panel.lastChildFill && index === (panel.children?.length ?? 0) - 1;
|
|
95
|
+
if (isFill) return { flex: '1 1 auto', minHeight: 0 };
|
|
96
|
+
// Left and right dock along the cross axis of the column the panel lays out, so they become a row
|
|
97
|
+
// of their own rather than a sibling in the column.
|
|
98
|
+
return dock === Dock.Left || dock === Dock.Right
|
|
99
|
+
? { alignSelf: dock === Dock.Left ? 'flex-start' : 'flex-end', flex: '0 0 auto' }
|
|
100
|
+
: { flex: '0 0 auto' };
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (isWrapPanel(panel)) {
|
|
104
|
+
if (panel.itemWidth === undefined && panel.itemHeight === undefined) return undefined;
|
|
105
|
+
return { width: panel.itemWidth, height: panel.itemHeight, flex: '0 0 auto' };
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return undefined;
|
|
109
|
+
}
|