Debian Codegraph @

ivette/src/dome/renderer/controls/icons.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 // --------------------------------------------------------------------------
24 // --- SVG Icons
25 // --------------------------------------------------------------------------
26
27 /**
28 You can [register](#.register) new icons or override existing ones
29 and [iterate](#.forEach) over the icon base.
30
31 [[include:icons.md]]
32
33 @packageDocumentation
34 @module dome/controls/icons
35 */
36
37 import _ from 'lodash';
38 import React from 'react';
39 import { classes } from 'dome/misc/utils';
40 import Gallery from './gallery.json';
41 import './style.css';
42
43 /* @ internal */
44 const Icons: { [id: string]: { viewBox?: string; path: string } } = Gallery;
45
46 // --------------------------------------------------------------------------
47 // --- Raw SVG element
48 // --------------------------------------------------------------------------
49
50 export interface SVGprops {
51 /** Icon's identifier. */
52 id: string;
53 /** Icon's tool-tip. */
54 title?: string;
55 /** Icon's dimension in pixels (default: `12`). */
56 size?: number;
57 /**
58 Vertical alignement offset, in pixels.
59 Default is set to `-0.125` times the size).
60 */
61 offset?: number;
62 className?: string;
63 fill?: string;
64 }
65
66 /**
67 Low-level SVG icon.
68 Only returns the identified `<svg/>` element from Icon base.
69 */
70 export function SVG(props: SVGprops): null | JSX.Element {
71 const { id, className, fill } = props;
72 if (!id) return null;
73 const icon = Icons[id];
74 if (!icon) return <>{id}</>;
75 const {
76 title,
77 size = 12,
78 offset = _.floor(-size * 0.125),
79 } = props;
80 const { path, viewBox = '0 0 24 24' } = icon;
81 return (
82 <svg
83 height={size}
84 width={size}
85 style={{ bottom: offset }}
86 viewBox={viewBox}
87 className = {className}
88 >
89 {title && <title>{title}</title>}
90 <path d={path} fill={fill} />
91 </svg>
92 );
93 }
94
95 // --------------------------------------------------------------------------
96 // --- Icon Component
97 // --------------------------------------------------------------------------
98
99 /** Icon Component Properties */
100 export interface IconProps extends SVGprops {
101 /** Additional class name(s). */
102 className?: string;
103 /** Additional DIV style. */
104 style?: React.CSSProperties;
105 /** Fill style property. */
106 fill?: string;
107 /** Click callback. */
108 onClick?: (event?: React.MouseEvent<HTMLDivElement>) => void;
109 }
110
111 /**
112 Icon Component.
113 Consult the [Icon Gallery](../guides/icons.md.html) for default icons.
114 */
115 export function Icon(props: IconProps): JSX.Element {
116 const {
117 id, title, onClick, fill,
118 size, className = '', offset, style,
119 } = props;
120 const divClass = classes('dome-xIcon', className);
121 const divStyle = fill ? { fill, ...style } : style;
122 return (
123 <div
124 className={divClass}
125 style={divStyle}
126 onClick={onClick}
127 >
128 <SVG id={id} size={size} title={title} offset={offset} />
129 </div>
130 );
131 }
132
133 // --------------------------------------------------------------------------
134 // --- Badge Component
135 // --------------------------------------------------------------------------
136
137 /** Badge Properties */
138 export interface BadgeProps {
139 /**
140 Displayed value.
141 Can be a number, a single letter, or an icon identifier.
142 */
143 value: number | string;
144 /** Badge tool-tip. */
145 title?: string;
146 /** Click callback. */
147 onClick?: (event?: React.MouseEvent) => void;
148 }
149
150 /**
151 Rounded icon, number or letter.
152 Depending on the type of value, display either a number,
153 a label, or the corresponding named icon.
154 Consult the [Icon Gallery](gallery-icons.html) for default icons.
155 */
156 export function Badge(props: BadgeProps): JSX.Element {
157 const { value, title, onClick } = props;
158 let content;
159 if (typeof value === 'string' && Icons[value]) {
160 content = <Icon id={value} size={10} />;
161 } else {
162 const style =
163 (typeof (value) === 'number' && value < 10) ||
164 (typeof (value) === 'string' && value.length === 1) ?
165 { paddingLeft: 2, paddingRight: 2 } : {};
166 content = <label style={style} className="dome-text-label">{value}</label>;
167 }
168 return (
169 <div
170 className="dome-xBadge"
171 title={title}
172 onClick={onClick}
173 >
174 {content}
175 </div>
176 );
177 }
178
179 // --------------------------------------------------------------------------
180 // --- Icon Database
181 // --------------------------------------------------------------------------
182
183 /** Custom Icon Properties */
184 export interface CustomIcon {
185 /** Icon identifier. */
186 id: string;
187 /** Icon's viewBox (default is `"0 0 24 24"`). */
188 viewBox?: string;
189 /** Icon's SVG path. */
190 path: string;
191 }
192
193 /**
194 Register a new custom icon.
195 */
196 export function register(icon: CustomIcon): void {
197 const { id, ...jsicon } = icon;
198 Icons[id] = jsicon;
199 }
200
201 /**
202 Iterate over icons gallery.
203 See [[register]] to add custom icons to the gallery.
204 */
205 export function forEach(fn: (ico: CustomIcon) => void): void {
206 const ids = Object.keys(Icons);
207 ids.forEach((id) => {
208 const jsicon = Icons[id];
209 fn({ id, ...jsicon });
210 });
211 }
212
213 // --------------------------------------------------------------------------