-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathIcons.tsx
238 lines (224 loc) · 6.98 KB
/
Icons.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import React, { useState, useEffect } from 'react';
import {
dh,
type IconDefinition,
vsOrganization,
dhSquareFilled,
dhAddSmall,
} from '@deephaven/icons';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { Button, Icon } from '@deephaven/components';
import PropTypes from 'prop-types';
import SampleSection from './SampleSection';
async function copyText(text: string): Promise<void | ErrorConstructor> {
try {
return await navigator.clipboard.writeText(text);
} catch (error) {
throw new Error(`Unable to copy: ${error}`);
}
}
const DH_PREFIX = 'dh';
const VS_PREFIX = 'vs';
// prefix, icon-name to prefixIconName
const getPrefixedName = (name: string, prefix: string): string =>
prefix.toLowerCase() +
name
.split('-')
.map(str => str.charAt(0).toUpperCase() + str.slice(1))
.join('');
interface FlashProps {
message: { text: React.ReactNode };
}
function Flash({ message, message: { text } }: FlashProps): JSX.Element {
const [show, setShow] = useState(false);
useEffect(
function setFlashMessage() {
if (text == null || text === '') return;
setShow(true);
const timeout = setTimeout(() => {
setShow(false);
}, 2000);
// eslint-disable-next-line consistent-return
return () => {
clearTimeout(timeout);
};
},
[message, text]
);
return <p className={show ? 'flash in' : 'flash out'}>{text}</p>;
}
Flash.propTypes = {
message: PropTypes.shape({
text: PropTypes.node,
}).isRequired,
};
function Icons(): React.ReactElement {
const [dhFilter, setDhFilter] = useState<boolean>(true);
const [vsFilter, setVsFilter] = useState<boolean>(true);
const [search, setSearch] = useState<string>('');
const [flashText, setFlashText] = useState<{ text: React.ReactNode }>({
text: '',
});
const renderIcons = Object.values(dh)
.filter((icon: IconDefinition): boolean => {
const matchesFilter =
getPrefixedName(icon.iconName, icon.prefix)
.toLowerCase()
.indexOf(
search.replace(/\s/g, '').replace(/-/g, '').toLowerCase()
) !== -1;
const isDH = dhFilter && (icon.prefix as string) === DH_PREFIX;
const isVS = vsFilter && (icon.prefix as string) === VS_PREFIX;
return matchesFilter && (isDH || isVS);
})
.map(icon => {
const prefixedName = getPrefixedName(icon.iconName, icon.prefix);
return (
<Button
key={prefixedName}
kind="inline"
className="card"
onClick={() => {
// new object, so it always flashes even on same string
copyText(prefixedName)
.then(() => {
setFlashText({
text: (
<span>
<FontAwesomeIcon icon={dh.vsOutput} /> Copied text:{' '}
<strong>{prefixedName}</strong>
</span>
),
});
})
.catch(err => {
setFlashText({
text: <span className="text-danger">{err.message}</span>,
});
});
}}
>
<Icon size="L">
<FontAwesomeIcon icon={icon} />
</Icon>
<label title={prefixedName}>{prefixedName}</label>
</Button>
);
});
const compositionExample = `
<div className="fa-md fa-layers">
<FontAwesomeIcon
mask={vsOrganization}
icon={dhSquareFilled}
transform="down-7 right-7"
/>
<FontAwesomeIcon icon={dhAddSmall} />
</div>`;
return (
<SampleSection name="icons">
<h2 className="ui-title">Icons</h2>
<div className="row">
<div className="col">
<h4>Icon Composition</h4>
<p>
Icons can be used indivudally or composed together using
font-awesome composition:
</p>
<div className="icons">
<div className="icon card">
<FontAwesomeIcon icon={vsOrganization} />
</div>
<div className="icon card">
<FontAwesomeIcon icon={dhSquareFilled} />
</div>
<div className="icon card">
<FontAwesomeIcon icon={dhAddSmall} />
</div>
<div className="icon card">=</div>
<Button
kind="inline"
className="card"
onClick={() => {
// new object, so it always flashes even on same string
copyText(compositionExample)
.then(() => {
setFlashText({
text: <span>Copied composition example</span>,
});
})
.catch(err => {
setFlashText({
text: <span className="text-danger">{err.message}</span>,
});
});
}}
>
<div className="icon">
<div className="fa-md fa-layers">
<FontAwesomeIcon
mask={vsOrganization}
icon={dhSquareFilled}
transform="down-7 right-7"
/>
<FontAwesomeIcon icon={dhAddSmall} />
</div>
</div>
<label>Custom</label>
</Button>
</div>
</div>
</div>
<hr />
<div className="row">
<div className="col">
<h4>All available icons</h4>
<p>
If you cannot find or compose a relevant icon for your use case,
please request a new one to be created from design.
</p>
<div className="form-inline mb-3">
<input
type="search"
placeholder="Basic icon search..."
value={search}
className="form-control"
onChange={event => {
setSearch(event.target.value);
}}
/>
<span className="mx-2">Show: </span>
<Button
kind="inline"
active={vsFilter}
className="mr-2"
onClick={() => setVsFilter(!vsFilter)}
icon={vsFilter ? dh.vsCheck : dh.vsClose}
>
VS ICONS
</Button>
<Button
kind="inline"
active={dhFilter}
className="mr-2"
onClick={() => setDhFilter(!dhFilter)}
icon={dhFilter ? dh.vsCheck : dh.vsClose}
>
DH ICONS
</Button>
<small>
({renderIcons.length} icon{renderIcons.length === 1 ? '' : 's'})
</small>
</div>
<div className="icons">
{renderIcons}
{renderIcons.length === 0 && (
<p className="no-result">No icons found.</p>
)}
<Flash message={flashText} />
</div>
</div>
</div>
</SampleSection>
);
}
export default Icons;