import React, { useCallback, CSSProperties } from 'react'; import { useEditor } from '@craftjs/core'; import { BG_COLORS, RADIUS_PRESETS, SPACING_PRESETS, SHADOW_PRESETS, } from '../../../constants/presets'; import { StylePanelProps, SectionLabel, ColorSwatchGrid, PresetButtonGrid, TextInputField, ColorPickerField, CollapsibleSection, SpacingControl, SpacingSide, BorderControl, BorderValue, buildBorderShorthand, AnimationControl, VisibilityControl, sectionGap, labelStyle, autoTextColor, useNodeProp, } from './shared'; function capitalize(s: string): string { return s.charAt(0).toUpperCase() + s.slice(1); } /** Parses a `border` shorthand string (e.g. "2px solid #ff0000") back into * the {width,style,color} shape BorderControl edits. Only needs to * round-trip values this same panel produced via buildBorderShorthand. */ function parseBorderShorthand(v: string | undefined): BorderValue { if (!v || v === 'none') return { width: '', style: 'none', color: '#000000' }; const m = String(v).trim().match(/^(\d+(?:\.\d+)?(?:px|em|rem)?)\s+(\w+)\s+(.+)$/); if (!m) return { width: '', style: 'none', color: '#000000' }; return { width: m[1], style: m[2], color: m[3] }; } /** style.opacity is a CSS-length-free numeric string ("0.8") or blank * (treated as fully opaque). Converts to a 0-100 integer for the UI. */ function opacityPercent(v: unknown): number { if (v === undefined || v === null || v === '') return 100; const n = parseFloat(String(v)); return Number.isFinite(n) ? Math.round(n * 100) : 100; } /* ---------- BUTTON ---------- */ export const ButtonStylePanel: React.FC = ({ selectedId, nodeProps }) => { const { actions } = useEditor(); const style: CSSProperties = nodeProps.style || {}; const { setProp, setPropStyle } = useNodeProp(selectedId); const setButtonColor = useCallback( (bgColor: string) => { actions.setProp(selectedId, (props: any) => { props.style = { ...props.style, backgroundColor: bgColor, color: autoTextColor(bgColor), }; }); }, [actions, selectedId], ); return ( <>
Button Color
{ actions.setProp(selectedId, (props: any) => { props.text = v; }); }} /> { actions.setProp(selectedId, (props: any) => { props.href = v; }); }} />
Border Radius setPropStyle('borderRadius', v)} />
Padding setPropStyle('padding', v)} />
{/* Hover state -- rendered into a scoped