PluginProbe
Slider Block by Sliderberg – WordPress Slider & Carousel Plugin for Gutenberg / trunk
Slider Block by Sliderberg – WordPress Slider & Carousel Plugin for Gutenberg vtrunk
1.2.3 1.2.2 1.2.0 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.7 1.0.8 1.0.9
sliderberg / shared / src / components / FontAppearanceControlWithToolsPanel.tsx

FontAppearanceControlWithToolsPanel.tsx in Slider Block by Sliderberg – WordPress Slider & Carousel Plugin for Gutenberg trunk, at shared/src/components/FontAppearanceControlWithToolsPanel.tsx

107 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import React from "react";
2
3 import {
4 __experimentalFontAppearanceControl as WPFontAppearanceControl,
5 useBlockEditContext,
6 } from "@wordpress/block-editor";
7 import { __experimentalToolsPanelItem as ToolsPanelItem } from "@wordpress/components";
8 import { useDispatch, useSelect } from "@wordpress/data";
9
10 import type {
11 FontAppearanceControlWithToolsPanelProps,
12 FontAppearanceValue,
13 } from "./types";
14
15 const DEFAULT_FONT_APPEARANCE: FontAppearanceValue = {
16 fontStyle: "normal",
17 fontWeight: "400",
18 };
19
20 function normalizeFontAppearance(
21 value: unknown,
22 defaultValue: FontAppearanceValue,
23 ): FontAppearanceValue {
24 if (value && typeof value === "object") {
25 const appearance = value as FontAppearanceValue;
26
27 return {
28 fontStyle: appearance.fontStyle || defaultValue.fontStyle,
29 fontWeight: appearance.fontWeight || defaultValue.fontWeight,
30 };
31 }
32
33 return defaultValue;
34 }
35
36 const FontAppearanceControlWithToolsPanel: React.FC<
37 FontAppearanceControlWithToolsPanelProps
38 > = ({
39 attrKey,
40 label,
41 defaultValue = DEFAULT_FONT_APPEARANCE,
42 onAttributesUpdate = () => null,
43 hasFontStyles = true,
44 hasFontWeights = true,
45 fontFamilyFaces,
46 isShownByDefault = true,
47 }) => {
48 const { clientId } = useBlockEditContext();
49 const { updateBlockAttributes } = useDispatch("core/block-editor") as any;
50
51 const attributes = useSelect(
52 (select) => {
53 const blockEditorStore = select("core/block-editor") as any;
54
55 return blockEditorStore?.getBlockAttributes?.(clientId) ?? {};
56 },
57 [clientId],
58 ) as Record<string, any>;
59
60 const currentValue = normalizeFontAppearance(
61 attributes[attrKey],
62 defaultValue,
63 );
64
65 const setAttributes = (newAttributes: Record<string, any>) => {
66 updateBlockAttributes(clientId, newAttributes);
67 onAttributesUpdate(newAttributes);
68 };
69
70 const hasValue = () => {
71 const val = attributes[attrKey];
72 if (!val) return false;
73 return (
74 val.fontStyle !== defaultValue.fontStyle ||
75 val.fontWeight !== defaultValue.fontWeight
76 );
77 };
78
79 return (
80 <ToolsPanelItem
81 panelId={clientId}
82 isShownByDefault={isShownByDefault}
83 label={label}
84 hasValue={hasValue}
85 onDeselect={() => setAttributes({ [attrKey]: defaultValue })}
86 resetAllFilter={() => setAttributes({ [attrKey]: defaultValue })}
87 >
88 <WPFontAppearanceControl
89 __next40pxDefaultSize
90 label={label}
91 value={currentValue}
92 onChange={(newValue) =>
93 setAttributes({
94 [attrKey]:
95 normalizeFontAppearance(newValue, defaultValue) ?? defaultValue,
96 })
97 }
98 hasFontStyles={hasFontStyles}
99 hasFontWeights={hasFontWeights}
100 fontFamilyFaces={fontFamilyFaces}
101 />
102 </ToolsPanelItem>
103 );
104 };
105
106 export default FontAppearanceControlWithToolsPanel;
107