PluginProbe
Tableberg – Simple Gutenberg Table Block / 1.2.0
Tableberg – Simple Gutenberg Table Block v1.2.0
1.2.0 1.1.5 1.1.4 1.1.3 1.1.2 1.1.1 1.1.0 1.0.5 1.0.4 1.0.3 1.0.2 1.0.1 trunk 0.0.2 0.2.1 0.3.2 0.3.3 0.4.1 0.5.0 0.5.1 0.5.2 0.5.3 0.5.4 0.5.5 0.5.6 All 43 releases
tableberg / components / editor / useColorPalettes.ts

useColorPalettes.ts in Tableberg – Simple Gutenberg Table Block 1.2.0, at components/editor/useColorPalettes.ts

51 lines 1.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { __ } from "@wordpress/i18n";
2 import { store as blockEditorStore } from "@wordpress/block-editor";
3 import { useSelect } from "@wordpress/data";
4 import { useMemo } from "react";
5
6 export interface PaletteColor {
7 name: string;
8 slug?: string;
9 color: string;
10 }
11
12 export interface PaletteOrigin {
13 name: string;
14 colors: PaletteColor[];
15 }
16
17 /**
18 * Every colour origin the editor knows about, as palette groups.
19 *
20 * `useMultipleOriginColorsAndGradients` drops the Default palette whenever a
21 * theme sets `defaultPalette: false`, and these controls have always shown
22 * those core swatches. Building the list here keeps them while adding the
23 * theme's own Styles palette next to them.
24 */
25 export function useColorPalettes(): PaletteOrigin[] {
26 const palette = useSelect(select => {
27 const settings = (
28 select(blockEditorStore) as BlockEditorStoreSelectors
29 ).getSettings();
30
31 return settings?.__experimentalFeatures?.color?.palette;
32 }, []);
33
34 return useMemo(() => {
35 // `custom` is not in the editor's type for the palette, but it is
36 // there at runtime once someone saves their own colours.
37 const custom = (palette as { custom?: PaletteColor[] } | undefined)
38 ?.custom;
39
40 const origins: Array<[string, PaletteColor[] | undefined]> = [
41 [__("Theme", "tableberg"), palette?.theme],
42 [__("Default", "tableberg"), palette?.default],
43 [__("Custom", "tableberg"), custom],
44 ];
45
46 return origins
47 .filter(([, colors]) => !!colors?.length)
48 .map(([name, colors]) => ({ name, colors: colors as PaletteColor[] }));
49 }, [palette]);
50 }
51