forked from 10up/block-components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
56 lines (45 loc) · 1.09 KB
/
index.js
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
import { useSelect } from '@wordpress/data';
import { useState, useEffect } from '@wordpress/element';
import { iconStore } from '../../stores';
function transformIcons(iconSet) {
return iconSet.icons.map((icon) => ({ ...icon, iconSet: iconSet.name }));
}
const useIcons = (iconSet = false) => {
const [icons, setIcons] = useState([]);
const rawIcons = useSelect(
(select) => {
const { getIconSet, getIconSets } = select(iconStore);
if (iconSet) {
return getIconSet(iconSet);
}
return getIconSets();
},
[iconSet],
);
useEffect(() => {
if (iconSet) {
setIcons(transformIcons(rawIcons));
}
setIcons(
Object.values(rawIcons).reduce(
(rawIcons, iconSet) => [...rawIcons, ...transformIcons(iconSet)],
[],
),
);
}, [rawIcons, iconSet]);
return icons;
};
const useIcon = (iconSet, name) => {
const [icon, setIcon] = useState(null);
const rawIcon = useSelect(
(select) => {
return select(iconStore).getIcon(iconSet, name);
},
[iconSet, name],
);
useEffect(() => {
setIcon(rawIcon);
}, [rawIcon]);
return icon;
};
export { useIcons, useIcon };