-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathicon-picker.tsx
More file actions
233 lines (206 loc) · 5.7 KB
/
Copy pathicon-picker.tsx
File metadata and controls
233 lines (206 loc) · 5.7 KB
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
import styled from '@emotion/styled';
import { __ } from '@wordpress/i18n';
import {
CheckboxControl,
BaseControl,
NavigableMenu,
SearchControl,
Tooltip,
} from '@wordpress/components';
import { useInstanceId } from '@wordpress/compose';
import { useState, memo, useMemo, forwardRef } from '@wordpress/element';
import { FixedSizeGrid as Grid, areEqual } from 'react-window';
import { useIcons } from '../../hooks/use-icons';
import { useFilteredList } from '../../hooks/use-filtered-list';
import { Icon } from './icon';
/**
* TooltipContent
*
* The `@wordpress/components` Tooltip component tries to clone the child element
* passed into it. This child will get some additional children passed in. In some cases
* this clashes with elements that use dangerouslySetInnerHTML. This component is a
* workaround for that. It will just wrap the children in a div and pass that to the
* Tooltip component.
*/
const TooltipContent = forwardRef<HTMLDivElement, React.ComponentPropsWithoutRef<'div'>>(
function TooltipContent(props, ref) {
const { children } = props;
return (
<div ref={ref} className="component-icon-picker__tooltip-content" {...props}>
{children}
</div>
);
},
);
const StyledIconButton = styled(Icon)`
background-color: ${({ selected }: { selected: boolean }) => (selected ? 'black' : 'white')};
color: ${({ selected }: { selected: boolean }) => (selected ? 'white' : 'black')};
fill: ${({ selected }: { selected: boolean }) => (selected ? 'white' : 'black')};
padding: 5px;
border: none;
border-radius: 4px;
height: 34px;
width: 34px;
display: flex;
align-items: center;
justify-content: center;
&:hover {
background-color: ${({ selected }) => (selected ? '#555D66' : '#f3f4f5')};
}
& svg {
max-height: 100%;
max-width: 100%;
height: auto;
width: 100%;
object-fit: contain;
}
`;
interface IconLabelProps {
/**
* Icon object
*/
icon: { name: string; iconSet: string; label: string };
/**
* Whether the icon is checked
*/
isChecked: boolean;
}
const IconLabel: React.FC<IconLabelProps> = (props) => {
const { icon, isChecked } = props;
return (
<Tooltip text={icon.label}>
<TooltipContent>
<StyledIconButton
selected={isChecked}
key={icon.name}
name={icon.name}
iconSet={icon.iconSet}
/>
</TooltipContent>
</Tooltip>
);
};
interface IconGridItemProps {
/**
* Column index
*/
columnIndex: number;
/**
* Row index
*/
rowIndex: number;
/**
* Style object
*/
style: React.CSSProperties;
/**
* Data object
*/
data: unknown;
}
const IconGridItem = memo<IconGridItemProps>((props) => {
const { columnIndex, rowIndex, style, data } = props;
const { icons, selectedIcon, onChange } = data as {
icons: { name: string; iconSet: string; label: string }[];
selectedIcon: { name: string; iconSet: string };
onChange: (icon: { name: string; iconSet: string }) => void;
};
const index = rowIndex * 5 + columnIndex;
const icon = icons[index];
const isChecked = selectedIcon?.name === icon?.name && selectedIcon?.iconSet === icon?.iconSet;
if (!icon) {
return null;
}
// We need to cast the IconLabel to a string because types in WP are not correct
const label = (<IconLabel isChecked={isChecked} icon={icon} />) as unknown as string;
return (
<div style={style}>
<CheckboxControl
key={icon.name}
label={label}
checked={isChecked}
onChange={() => onChange(icon)}
className="component-icon-picker__checkbox-control"
/>
</div>
);
}, areEqual);
const StyledIconGrid = styled(Grid)`
.component-icon-picker__checkbox-control {
margin-bottom: 0;
}
.components-checkbox-control__input,
.components-checkbox-control__input-container {
display: none;
}
`;
interface IconGridProps {
/**
* List of icons
*/
icons: { name: string; iconSet: string; label: string }[];
/**
* Selected icon
*/
selectedIcon: { name: string; iconSet: string };
/**
* Change handler for when a new icon is selected
*/
onChange: (icon: { name: string; iconSet: string }) => void;
}
const IconGrid: React.FC<IconGridProps> = (props) => {
const { icons, selectedIcon, onChange } = props;
const itemData = useMemo(
() => ({ icons, selectedIcon, onChange }),
[icons, selectedIcon, onChange],
);
return (
<NavigableMenu orientation="vertical" className="component-icon-picker__list">
<StyledIconGrid
columnCount={5}
columnWidth={248 / 5}
rowCount={Math.ceil(icons.length / 5)}
rowHeight={248 / 5}
itemData={itemData}
height={200}
width={248}
>
{IconGridItem}
</StyledIconGrid>
</NavigableMenu>
);
};
export type IconPickerProps = Omit<React.ComponentProps<typeof BaseControl>, 'children'> & {
/**
* Value of the selected icon
*/
value: { name: string; iconSet: string };
/**
* Change handler for when a new icon is selected
*/
onChange: (icon: { name: string; iconSet: string }) => void;
/**
* Optionally specify the icon set to use
* If not specified, all icon sets will be used
*/
iconSet?: string;
};
export const IconPicker: React.FC<IconPickerProps> = (props) => {
const { value, onChange, iconSet, label = '', ...rest } = props;
const icons = useIcons(iconSet ? iconSet : '');
const instanceId = useInstanceId(IconPicker);
const id = `icon-picker-${instanceId}`;
const [searchTerm, setSearchTerm] = useState('');
const [filteredIcons] = useFilteredList(icons, searchTerm);
const hasIcons = !!filteredIcons.length;
return (
<BaseControl label={label} id={id} className="component-icon-picker" {...rest}>
<SearchControl value={searchTerm} onChange={setSearchTerm} id={id} />
{hasIcons ? (
<IconGrid icons={filteredIcons} selectedIcon={value} onChange={onChange} />
) : (
<p>{__('No icons were found...')}</p>
)}
</BaseControl>
);
};