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 / SelectControlWithToolsPanel.tsx

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

71 lines 1.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * WordPress dependencies
3 */
4 import React from "react";
5 import { useBlockEditContext } from "@wordpress/block-editor";
6 import { useSelect, useDispatch } from "@wordpress/data";
7 import {
8 SelectControl,
9 __experimentalToolsPanelItem as ToolsPanelItem,
10 } from "@wordpress/components";
11
12 /**
13 * Internal dependencies
14 */
15 import { SelectControlWithToolsPanelProps } from "./types";
16
17 /**
18 * Select control component with ToolsPanel integration
19 * Self-managed via useBlockEditContext, wrapped in ToolsPanelItem
20 */
21 const SelectControlWithToolsPanel: React.FC<
22 SelectControlWithToolsPanelProps
23 > = ({
24 label,
25 attrKey,
26 options,
27 defaultValue = "",
28 help,
29 isShownByDefault = true,
30 }) => {
31 const { clientId } = useBlockEditContext();
32
33 const attributes = useSelect((select) =>
34 select("core/block-editor").getBlockAttributes(clientId),
35 ) as Record<string, any>;
36
37 const { updateBlockAttributes } = useDispatch("core/block-editor") as any;
38
39 const setAttributes = (newAttributes: Record<string, any>) => {
40 updateBlockAttributes(clientId, newAttributes);
41 };
42
43 const value = attributes[attrKey];
44
45 return (
46 <ToolsPanelItem
47 panelId={clientId}
48 isShownByDefault={isShownByDefault}
49 label={label}
50 hasValue={() => value !== defaultValue}
51 onDeselect={() => setAttributes({ [attrKey]: defaultValue })}
52 resetAllFilter={() => setAttributes({ [attrKey]: defaultValue })}
53 >
54 <SelectControl
55 label={label}
56 value={value}
57 options={options}
58 onChange={(newValue: any) => {
59 setAttributes({ [attrKey]: newValue });
60 }}
61 help={help}
62 size={"__unstable-large"}
63 __next40pxDefaultSize
64 __nextHasNoMarginBottom
65 />
66 </ToolsPanelItem>
67 );
68 };
69
70 export default SelectControlWithToolsPanel;
71