Debian Codegraph @

ivette/src/dome/renderer/layout/boxes.tsx

raw
1 /* ************************************************************************ */
2 /* */
3 /* This file is part of Frama-C. */
4 /* */
5 /* Copyright (C) 2007-2022 */
6 /* CEA (Commissariat à l'énergie atomique et aux énergies */
7 /* alternatives) */
8 /* */
9 /* you can redistribute it and/or modify it under the terms of the GNU */
10 /* Lesser General Public License as published by the Free Software */
11 /* Foundation, version 2.1. */
12 /* */
13 /* It is distributed in the hope that it will be useful, */
14 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
15 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
16 /* GNU Lesser General Public License for more details. */
17 /* */
18 /* See the GNU Lesser General Public License version 2.1 */
19 /* for more details (enclosed in the file licenses/LGPLv2.1). */
20 /* */
21 /* ************************************************************************ */
22
23 /* eslint-disable @typescript-eslint/explicit-function-return-type */
24
25 // --------------------------------------------------------------------------
26 // --- Box Layout
27 // --------------------------------------------------------------------------
28
29 /**
30 This modules offers several `<div>` containers with various
31 predefined layout.
32
33 Boxes are the very elementary way to layout components horizontally
34 or vertically. The different kinds of proposed boxes differ in how they
35 extends in both directions: normal boxes extends
36 along their layout direction, _pack_ boxes don't extends and _fill_ boxes
37 extends along both directions.
38
39 Grids layout their component from left-to-right inside predefined _columns_,
40 then vertically by wrapping cells in rows.
41
42 The various containers layout and extensibility is listed below:
43 - [[Hbox]] horizontal, fixed height
44 - [[Vbox]] vertical, fixed width
45 - [[Hpack]] horizontal, fixed dimensions
46 - [[Vpack]] vertical, fixed dimensions
47 - [[Hfill]] horizontal, extends in both directions
48 - [[Vfill]] vertical, extends in both directions
49 - [[Grid]] uses CSS grid columns, extends in both directions
50 - [[Scroll]] scrolls its content
51
52 Inside a box, you may add `<Space/>` and `<Filler/>` to separate items.
53 Inside a grid, you may also use `<Space/>` or an empty `<div/>` for empty
54 cells.
55
56 <strong>Warning:</strong> large elements will be clipped if they overflow.
57 If you want to add scrolling capabilities to some item that does not manage
58 overflow natively, place it inside a `<Scroll/>` sub-container.
59
60 @packageDocumentation
61 @module dome/layout/boxes
62 */
63
64 import React from 'react';
65 import * as Dome from 'dome';
66 import { Title } from 'dome/controls/labels';
67 import { classes, styles } from 'dome/misc/utils';
68 import './style.css';
69
70 // --------------------------------------------------------------------------
71 // --- Generic Box
72 // --------------------------------------------------------------------------
73
74 /** Div properties that you can also use in boxes. */
75 export type DivProps = React.HTMLAttributes<HTMLDivElement>;
76
77 const makeBox = (
78 boxClasses: string,
79 props: DivProps,
80 morestyle?: React.CSSProperties,
81 ) => {
82 const { children, className, style, ...others } = props;
83 const allClasses = classes(className, boxClasses);
84 const allStyles = styles(style, morestyle);
85 return (
86 <div className={allClasses} style={allStyles} {...others}>
87 {children}
88 </div>
89 );
90 };
91
92 // --------------------------------------------------------------------------
93 // --- Predefined Defaults
94 // --------------------------------------------------------------------------
95
96 /**
97 Horizontal box (extends horizontally, no overflow).
98 */
99 export const Hbox =
100 (props: DivProps) => makeBox('dome-xBoxes-hbox dome-xBoxes-box', props);
101
102 /**
103 Vertical box (extends vertically, no overflow).
104 */
105 export const Vbox =
106 (props: DivProps) => makeBox('dome-xBoxes-vbox dome-xBoxes-box', props);
107
108 /**
109 Compact Horizontal box (fixed dimensions, no overflow).
110 */
111 export const Hpack =
112 (props: DivProps) => makeBox('dome-xBoxes-hbox dome-xBoxes-pack', props);
113
114 /**
115 Compact Vertical box (fixed dimensions, no overflow).
116 */
117 export const Vpack =
118 (props: DivProps) => makeBox('dome-xBoxes-vbox dome-xBoxes-pack', props);
119
120 /**
121 Horizontally filled box (fixed height, maximal width, no overflow).
122 */
123 export const Hfill =
124 (props: DivProps) => makeBox('dome-xBoxes-hbox dome-xBoxes-fill', props);
125
126 /**
127 Vertically filled box (fixed width, maximal height, no overflow).
128 */
129 export const Vfill =
130 (props: DivProps) => makeBox('dome-xBoxes-vbox dome-xBoxes-fill', props);
131
132 // --------------------------------------------------------------------------
133 // --- Scrolling & Spacing
134 // --------------------------------------------------------------------------
135
136 /**
137 Scrolling container.
138 */
139 export const Scroll =
140 (props: DivProps) => makeBox('dome-xBoxes-scroll dome-container', props);
141
142 /**
143 Rigid space between items in a box.
144 */
145 export const Space =
146 (props: DivProps) => makeBox('dome-xBoxes-space', props);
147
148 /**
149 Extensible space between items in a box.
150 */
151 export const Filler =
152 (props: DivProps) => makeBox('dome-xBoxes-filler', props);
153
154 // --------------------------------------------------------------------------
155 // --- Grids
156 // --------------------------------------------------------------------------
157
158 export interface GridProps extends DivProps { columns?: string }
159
160 /**
161 Grid box container.
162
163 Layout its children in a multi-column grid. Each column is specified by its
164 width, following the CSS Grid Layout `grid-template-columns` property.
165
166 The rows are populated with children from left-to-right, using one column at
167 a time. Items layout can be modified by using CSS Grid Layout item
168 properties.
169
170 Example: `<Grid columns="25% auto auto"> ... </Grid>`
171 */
172 export const Grid = (props: GridProps) => {
173 const { columns, ...others } = props;
174 return makeBox('dome-xBoxes-grid', others, { gridTemplateColumns: columns });
175 };
176
177 // --------------------------------------------------------------------------
178 // --- Folders
179 // --------------------------------------------------------------------------
180
181 export interface FolderProps {
182 /** Title bar label. */
183 label: string;
184 /** Title bar tooltip. */
185 title?: string;
186 /** Window settings key. */
187 settings?: string;
188 /** Default state (`false`). */
189 defaultUnfold?: boolean;
190 /** Contents left margin (in pixels, defaults to 18). */
191 indent?: number;
192 /** Children JSX elements. */
193 children?: React.ReactNode;
194 }
195
196 /**
197 Foldable (vertical, packed) box.
198 The head label is clickable to fold/unfold its contents.
199 */
200 export const Folder = (props: FolderProps) => {
201 const {
202 settings,
203 defaultUnfold = false,
204 indent = 18,
205 label, title, children,
206 } = props;
207 const [unfold, flip] = Dome.useFlipSettings(settings, defaultUnfold);
208 const icon = unfold ? 'TRIANGLE.DOWN' : 'TRIANGLE.RIGHT';
209 const display = unfold ? 'block' : 'none';
210 return (
211 <Vpack>
212 <Hpack onClick={flip}>
213 <Title icon={icon} label={label} title={title} />
214 </Hpack>
215 <Vpack style={{ display, marginLeft: indent }}>
216 {children}
217 </Vpack>
218 </Vpack>
219 );
220 };
221
222 // --------------------------------------------------------------------------