-
Notifications
You must be signed in to change notification settings - Fork 100
/
iconography.tsx
166 lines (153 loc) · 4.71 KB
/
iconography.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
import React, { useState } from 'react';
import { Link as ReactRouterLink } from 'react-router-dom';
import { useStyles } from 'sku/treat';
import didYouMean, { ReturnTypeEnums } from 'didyoumean2';
import { useBoxStyles } from '../../../../../lib/components/Box/useBoxStyles';
import {
Inline,
Box,
Text,
Stack,
Heading,
TextField,
IconSearch,
Strong,
} from '../../../../../lib/components';
import { Overlay } from '../../../../../lib/components/private/Overlay/Overlay';
import { Page } from '../../../types';
import * as icons from '../../../../../lib/components/icons';
import * as styleRefs from './iconography.treat';
type IconName = keyof typeof icons;
const iconNames = Object.keys(icons).map(icon => ({
name: icon as IconName,
displayName: icon.replace(/^Icon/, ''),
}));
const IconTile = ({
icon,
suggestion = false,
}: {
icon: typeof iconNames[number];
suggestion?: boolean;
}) => {
const styles = useStyles(styleRefs);
const IconComponent = icons[icon.name];
return (
<ReactRouterLink
to={`/components/${icon.name}`}
className={useBoxStyles({ component: 'a' })}
>
<Box
position="relative"
display={'flex'}
flexDirection="column"
alignItems="center"
paddingX="xxsmall"
paddingY="medium"
cursor="pointer"
className={styles.iconContainer}
>
<Box height="touchable" className={styles.icon}>
<IconComponent
size="fill"
tone={suggestion ? 'secondary' : undefined}
/>
</Box>
<Box paddingTop="medium">
<Text tone="secondary" size="xsmall">
{icon.displayName}
</Text>
</Box>
<Overlay
boxShadow="borderStandard"
borderRadius="standard"
transition="fast"
className={styles.overlay}
/>
<Overlay
boxShadow="medium"
borderRadius="standard"
transition="fast"
className={styles.overlay}
/>
</Box>
</ReactRouterLink>
);
};
const page: Page = {
title: 'Iconography',
Component: () => {
const [iconList, setIconList] = useState(iconNames);
const [searchTerm, setSearchTerm] = useState('');
const [isDisambiguated, setDisambiguated] = useState(false);
return (
<Stack space="large">
<Heading level="2">Iconography</Heading>
<Stack space="small">
<Stack space="large">
<TextField
id="iconSearch"
icon={<IconSearch />}
placeholder="Search"
autoComplete="off"
autoFocus={true}
reserveMessageSpace={false}
value={searchTerm}
onChange={({ currentTarget }) => {
const searchText = currentTarget.value;
setSearchTerm(searchText);
const filteredList = iconNames.filter(
({ name }) =>
searchText.length === 0 ||
name.toLowerCase().indexOf(searchText.toLowerCase()) > -1,
);
if (filteredList.length === 0) {
const suggestions =
didYouMean(searchText, iconNames, {
returnType: ReturnTypeEnums.ALL_CLOSEST_MATCHES,
matchPath: ['displayName'],
}) ?? [];
const suggestionList = Array.isArray(suggestions)
? suggestions
: [suggestions];
setDisambiguated(suggestionList && suggestionList.length > 0);
setIconList(suggestionList);
} else {
setDisambiguated(false);
setIconList(filteredList);
}
}}
/>
{isDisambiguated || iconList.length === 0 ? (
<Text tone="secondary">
No icons matching <Strong>`{searchTerm}`</Strong>
{isDisambiguated ? (
<span>
, did you mean{' '}
{iconList.length === 1 ? (
<Strong>`{iconList[0].displayName}`</Strong>
) : (
'one of these'
)}
:
</span>
) : (
'.'
)}
</Text>
) : null}
</Stack>
<Inline space={['none', 'medium']}>
{iconList.map(icon => (
<IconTile
key={icon.name}
icon={icon}
suggestion={isDisambiguated}
/>
))}
</Inline>
</Stack>
</Stack>
);
},
};
export default page;