Enh: text+button (typography depth, button new-tab + scoped hover, box-model+anim/vis) #15
@@ -52,3 +52,69 @@ describe('ButtonLink.toHtml text escaping (attacker-controlled `text` prop)', ()
|
||||
expect(html).toContain('>Click Me</a>');
|
||||
});
|
||||
});
|
||||
|
||||
describe('ButtonLink.toHtml hover state (scoped <style> block)', () => {
|
||||
test('no hover props -- no <style> block, no class added', () => {
|
||||
const { html } = toHtml({ href: '#', text: 'x' }, '', 'node-1');
|
||||
expect(html).not.toContain('<style>');
|
||||
expect(html).not.toContain('class=');
|
||||
});
|
||||
|
||||
test('hoverBg/hoverColor emit a scoped :hover rule scoped to the node id', () => {
|
||||
const { html } = toHtml({ href: '#', text: 'x', hoverBg: '#111111', hoverColor: '#eeeeee' }, '', 'node-42');
|
||||
expect(html).toMatch(/<style>\.btn_[a-z0-9]+:hover\{background-color:#111111;color:#eeeeee\}<\/style>/);
|
||||
expect(html).toMatch(/class="btn_[a-z0-9]+"/);
|
||||
});
|
||||
|
||||
test('two different node ids produce different scope classes (no collision)', () => {
|
||||
const a = toHtml({ href: '#', text: 'x', hoverBg: '#111111' }, '', 'node-a').html;
|
||||
const b = toHtml({ href: '#', text: 'x', hoverBg: '#111111' }, '', 'node-b').html;
|
||||
const scopeOf = (html: string) => html.match(/btn_[a-z0-9]+/)?.[0];
|
||||
expect(scopeOf(a)).toBeTruthy();
|
||||
expect(scopeOf(a)).not.toBe(scopeOf(b));
|
||||
});
|
||||
|
||||
test('an XSS breakout attempt in hoverBg cannot close the <style> element', () => {
|
||||
const malicious = '</style><script>alert(1)</script>';
|
||||
const { html } = toHtml({ href: '#', text: 'x', hoverBg: malicious }, '', 'node-1');
|
||||
expect(html).not.toContain('</style><script>');
|
||||
expect(html).not.toContain('<script>alert(1)</script>');
|
||||
});
|
||||
|
||||
test('a rule-breakout attempt in hoverColor cannot inject a second selector/rule', () => {
|
||||
const malicious = 'red;}body{background:red';
|
||||
const { html } = toHtml({ href: '#', text: 'x', hoverColor: malicious }, '', 'node-1');
|
||||
expect(html).not.toContain('}body{');
|
||||
expect(html).not.toContain(';}');
|
||||
// The whole export is still exactly one <style> element -- no new rule
|
||||
// or element was opened by the malicious value.
|
||||
expect((html.match(/<style>/g) || []).length).toBe(1);
|
||||
expect((html.match(/<\/style>/g) || []).length).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('ButtonLink.craft.props exposes target + hover + box-model + animation/visibility', () => {
|
||||
test('target defaults to _self, hoverBg/hoverColor blank', () => {
|
||||
const props = (ButtonLink as any).craft.props;
|
||||
expect(props.target).toBe('_self');
|
||||
expect(props.hoverBg).toBe('');
|
||||
expect(props.hoverColor).toBe('');
|
||||
});
|
||||
|
||||
test('animation, animationDelay, hideOnDesktop/Tablet/Mobile are present with blank/false defaults', () => {
|
||||
const props = (ButtonLink as any).craft.props;
|
||||
expect(props.animation).toBe('');
|
||||
expect(props.animationDelay).toBe('0');
|
||||
expect(props.hideOnDesktop).toBe(false);
|
||||
expect(props.hideOnTablet).toBe(false);
|
||||
expect(props.hideOnMobile).toBe(false);
|
||||
});
|
||||
|
||||
test('style carries blank/default box-model keys', () => {
|
||||
const style = (ButtonLink as any).craft.props.style;
|
||||
expect(style).toHaveProperty('marginTop');
|
||||
expect(style.border).toBe('none');
|
||||
expect(style.boxShadow).toBe('none');
|
||||
expect(style.opacity).toBe('1');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,13 +1,24 @@
|
||||
import React, { CSSProperties } from 'react';
|
||||
import { useNode, UserComponent } from '@craftjs/core';
|
||||
import { cssPropsToString } from '../../utils/style-helpers';
|
||||
import { escapeHtml, escapeAttr, safeUrl } from '../../utils/escape';
|
||||
import { escapeHtml, escapeAttr, safeUrl, cssValue, scopeId } from '../../utils/escape';
|
||||
|
||||
interface ButtonLinkProps {
|
||||
text?: string;
|
||||
href?: string;
|
||||
target?: '_self' | '_blank';
|
||||
style?: CSSProperties;
|
||||
/** Background color applied on `:hover` via a scoped `<style>` block
|
||||
* (editor preview does not show hover state -- only the published
|
||||
* export). Blank means "no hover background override". */
|
||||
hoverBg?: string;
|
||||
/** Text color applied on `:hover`, same scoped `<style>` block. */
|
||||
hoverColor?: string;
|
||||
animation?: string;
|
||||
animationDelay?: string;
|
||||
hideOnDesktop?: boolean;
|
||||
hideOnTablet?: boolean;
|
||||
hideOnMobile?: boolean;
|
||||
}
|
||||
|
||||
export const ButtonLink: UserComponent<ButtonLinkProps> = ({
|
||||
@@ -15,6 +26,8 @@ export const ButtonLink: UserComponent<ButtonLinkProps> = ({
|
||||
href = '#',
|
||||
target = '_self',
|
||||
style = {},
|
||||
hoverBg = '',
|
||||
hoverColor = '',
|
||||
}) => {
|
||||
const {
|
||||
connectors: { connect, drag },
|
||||
@@ -23,6 +36,8 @@ export const ButtonLink: UserComponent<ButtonLinkProps> = ({
|
||||
selected: node.events.selected,
|
||||
}));
|
||||
|
||||
const [hovered, setHovered] = React.useState(false);
|
||||
|
||||
return (
|
||||
<a
|
||||
ref={(ref: HTMLAnchorElement | null) => { if (ref) connect(drag(ref)); }}
|
||||
@@ -32,12 +47,16 @@ export const ButtonLink: UserComponent<ButtonLinkProps> = ({
|
||||
// Prevent navigation inside editor
|
||||
e.preventDefault();
|
||||
}}
|
||||
onMouseEnter={() => setHovered(true)}
|
||||
onMouseLeave={() => setHovered(false)}
|
||||
style={{
|
||||
display: 'inline-block',
|
||||
textDecoration: 'none',
|
||||
cursor: 'pointer',
|
||||
outline: selected ? '2px solid #3b82f6' : 'none',
|
||||
...style,
|
||||
...(hovered && hoverBg ? { backgroundColor: hoverBg } : {}),
|
||||
...(hovered && hoverColor ? { color: hoverColor } : {}),
|
||||
}}
|
||||
>
|
||||
{text}
|
||||
@@ -53,6 +72,8 @@ ButtonLink.craft = {
|
||||
text: 'Click Me',
|
||||
href: '#',
|
||||
target: '_self',
|
||||
hoverBg: '',
|
||||
hoverColor: '',
|
||||
style: {
|
||||
backgroundColor: '#3b82f6',
|
||||
color: '#ffffff',
|
||||
@@ -61,7 +82,15 @@ ButtonLink.craft = {
|
||||
fontWeight: '600',
|
||||
fontSize: '16px',
|
||||
border: 'none',
|
||||
marginTop: '', marginRight: '', marginBottom: '', marginLeft: '',
|
||||
boxShadow: 'none',
|
||||
opacity: '1',
|
||||
},
|
||||
animation: '',
|
||||
animationDelay: '0',
|
||||
hideOnDesktop: false,
|
||||
hideOnTablet: false,
|
||||
hideOnMobile: false,
|
||||
},
|
||||
rules: {
|
||||
canDrag: () => true,
|
||||
@@ -72,7 +101,7 @@ ButtonLink.craft = {
|
||||
|
||||
/* ---------- HTML export ---------- */
|
||||
|
||||
(ButtonLink as any).toHtml = (props: ButtonLinkProps, _childrenHtml: string) => {
|
||||
(ButtonLink as any).toHtml = (props: ButtonLinkProps, _childrenHtml: string, nodeId?: string) => {
|
||||
const styleStr = cssPropsToString({
|
||||
display: 'inline-block',
|
||||
textDecoration: 'none',
|
||||
@@ -80,7 +109,28 @@ ButtonLink.craft = {
|
||||
});
|
||||
const escapedText = escapeHtml(props.text || '');
|
||||
const targetAttr = props.target === '_blank' ? ' target="_blank" rel="noopener noreferrer"' : '';
|
||||
|
||||
// Scoped hover style -- same pattern as Navbar/Menu: a deterministic,
|
||||
// per-node class (via scopeId) avoids two ButtonLink instances on the
|
||||
// same page colliding on a shared `.btn-link:hover` rule. hoverBg/
|
||||
// hoverColor are sanitized through cssValue -- they land inside a
|
||||
// `<style>` element, the worst-case XSS sink (an unescaped `<`/`>` or
|
||||
// `{`/`}` could close the rule/element and open a `<script>`).
|
||||
const hoverBg = cssValue(props.hoverBg);
|
||||
const hoverColor = cssValue(props.hoverColor);
|
||||
let hoverCss = '';
|
||||
let cls = '';
|
||||
if (hoverBg || hoverColor) {
|
||||
const scope = scopeId(nodeId, (props.href || '') + (props.text || ''), 'btn');
|
||||
cls = ` class="${scope}"`;
|
||||
const decls = [
|
||||
hoverBg ? `background-color:${hoverBg}` : '',
|
||||
hoverColor ? `color:${hoverColor}` : '',
|
||||
].filter(Boolean).join(';');
|
||||
hoverCss = `<style>.${scope}:hover{${decls}}</style>`;
|
||||
}
|
||||
|
||||
return {
|
||||
html: `<a href="${escapeAttr(safeUrl(props.href || '#'))}"${targetAttr}${styleStr ? ` style="${styleStr}"` : ''}>${escapedText}</a>`,
|
||||
html: `${hoverCss}<a href="${escapeAttr(safeUrl(props.href || '#'))}"${targetAttr}${cls}${styleStr ? ` style="${styleStr}"` : ''}>${escapedText}</a>`,
|
||||
};
|
||||
};
|
||||
|
||||
@@ -56,3 +56,54 @@ describe('Heading.toHtml text escaping (attacker-controlled `text` prop)', () =>
|
||||
expect(html).toBe('<h2>Hello world</h2>');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Heading.toHtml typography depth (line-height/letter-spacing/transform/style/decoration)', () => {
|
||||
test('line-height, letter-spacing, text-transform all flow into the style attribute', () => {
|
||||
const { html } = toHtml({
|
||||
text: 'x',
|
||||
level: 'h2',
|
||||
style: { lineHeight: '1.25', letterSpacing: '0.05em', textTransform: 'uppercase' },
|
||||
}, '');
|
||||
expect(html).toContain('line-height:1.25');
|
||||
expect(html).toContain('letter-spacing:0.05em');
|
||||
expect(html).toContain('text-transform:uppercase');
|
||||
});
|
||||
|
||||
test('italic + underline toggles emit font-style and text-decoration', () => {
|
||||
const { html } = toHtml({
|
||||
text: 'x',
|
||||
level: 'h2',
|
||||
style: { fontStyle: 'italic', textDecoration: 'underline' },
|
||||
}, '');
|
||||
expect(html).toContain('font-style:italic');
|
||||
expect(html).toContain('text-decoration:underline');
|
||||
});
|
||||
|
||||
test('a custom font-size (not one of the presets) still flows through', () => {
|
||||
const { html } = toHtml({ text: 'x', level: 'h2', style: { fontSize: '42px' } }, '');
|
||||
expect(html).toContain('font-size:42px');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Heading.craft.props exposes the box-model + animation/visibility rollout', () => {
|
||||
test('animation, animationDelay, hideOnDesktop/Tablet/Mobile are present with blank/false defaults', () => {
|
||||
const props = (Heading as any).craft.props;
|
||||
expect(props.animation).toBe('');
|
||||
expect(props.animationDelay).toBe('0');
|
||||
expect(props.hideOnDesktop).toBe(false);
|
||||
expect(props.hideOnTablet).toBe(false);
|
||||
expect(props.hideOnMobile).toBe(false);
|
||||
});
|
||||
|
||||
test('style carries blank/default box-model + typography-depth keys', () => {
|
||||
const style = (Heading as any).craft.props.style;
|
||||
expect(style).toHaveProperty('marginTop');
|
||||
expect(style).toHaveProperty('paddingTop');
|
||||
expect(style).toHaveProperty('lineHeight');
|
||||
expect(style).toHaveProperty('letterSpacing');
|
||||
expect(style).toHaveProperty('textTransform');
|
||||
expect(style.border).toBe('none');
|
||||
expect(style.boxShadow).toBe('none');
|
||||
expect(style.opacity).toBe('1');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -100,7 +100,22 @@ Heading.craft = {
|
||||
fontFamily: 'Inter, sans-serif',
|
||||
color: '#1f2937',
|
||||
marginBottom: '16px',
|
||||
lineHeight: '',
|
||||
letterSpacing: '',
|
||||
textTransform: '' as CSSProperties['textTransform'],
|
||||
fontStyle: '' as CSSProperties['fontStyle'],
|
||||
textDecoration: '',
|
||||
marginTop: '', marginRight: '', marginLeft: '',
|
||||
paddingTop: '', paddingRight: '', paddingBottom: '', paddingLeft: '',
|
||||
border: 'none',
|
||||
boxShadow: 'none',
|
||||
opacity: '1',
|
||||
},
|
||||
animation: '',
|
||||
animationDelay: '0',
|
||||
hideOnDesktop: false,
|
||||
hideOnTablet: false,
|
||||
hideOnMobile: false,
|
||||
},
|
||||
rules: {
|
||||
canDrag: () => true,
|
||||
|
||||
@@ -20,3 +20,48 @@ describe('TextBlock.toHtml text escaping (attacker-controlled `text` prop)', ()
|
||||
expect(html).toBe('<p>Hello world</p>');
|
||||
});
|
||||
});
|
||||
|
||||
describe('TextBlock.toHtml typography depth (line-height/letter-spacing/transform/style/decoration)', () => {
|
||||
test('line-height, letter-spacing, text-transform all flow into the style attribute', () => {
|
||||
const { html } = toHtml({
|
||||
text: 'x',
|
||||
style: { lineHeight: '1.75', letterSpacing: '-0.02em', textTransform: 'capitalize' },
|
||||
}, '');
|
||||
expect(html).toContain('line-height:1.75');
|
||||
expect(html).toContain('letter-spacing:-0.02em');
|
||||
expect(html).toContain('text-transform:capitalize');
|
||||
});
|
||||
|
||||
test('italic + underline toggles emit font-style and text-decoration', () => {
|
||||
const { html } = toHtml({ text: 'x', style: { fontStyle: 'italic', textDecoration: 'underline' } }, '');
|
||||
expect(html).toContain('font-style:italic');
|
||||
expect(html).toContain('text-decoration:underline');
|
||||
});
|
||||
|
||||
test('a custom font-size (not one of the presets) still flows through', () => {
|
||||
const { html } = toHtml({ text: 'x', style: { fontSize: '19px' } }, '');
|
||||
expect(html).toContain('font-size:19px');
|
||||
});
|
||||
});
|
||||
|
||||
describe('TextBlock.craft.props exposes the box-model + animation/visibility rollout', () => {
|
||||
test('animation, animationDelay, hideOnDesktop/Tablet/Mobile are present with blank/false defaults', () => {
|
||||
const props = (TextBlock as any).craft.props;
|
||||
expect(props.animation).toBe('');
|
||||
expect(props.animationDelay).toBe('0');
|
||||
expect(props.hideOnDesktop).toBe(false);
|
||||
expect(props.hideOnTablet).toBe(false);
|
||||
expect(props.hideOnMobile).toBe(false);
|
||||
});
|
||||
|
||||
test('style carries blank/default box-model + typography-depth keys', () => {
|
||||
const style = (TextBlock as any).craft.props.style;
|
||||
expect(style).toHaveProperty('marginTop');
|
||||
expect(style).toHaveProperty('paddingTop');
|
||||
expect(style).toHaveProperty('letterSpacing');
|
||||
expect(style).toHaveProperty('textTransform');
|
||||
expect(style.border).toBe('none');
|
||||
expect(style.boxShadow).toBe('none');
|
||||
expect(style.opacity).toBe('1');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -83,7 +83,21 @@ TextBlock.craft = {
|
||||
fontSize: '16px',
|
||||
lineHeight: '1.6',
|
||||
color: '#3f3f46',
|
||||
letterSpacing: '',
|
||||
textTransform: '' as CSSProperties['textTransform'],
|
||||
fontStyle: '' as CSSProperties['fontStyle'],
|
||||
textDecoration: '',
|
||||
marginTop: '', marginRight: '', marginBottom: '', marginLeft: '',
|
||||
paddingTop: '', paddingRight: '', paddingBottom: '', paddingLeft: '',
|
||||
border: 'none',
|
||||
boxShadow: 'none',
|
||||
opacity: '1',
|
||||
},
|
||||
animation: '',
|
||||
animationDelay: '0',
|
||||
hideOnDesktop: false,
|
||||
hideOnTablet: false,
|
||||
hideOnMobile: false,
|
||||
},
|
||||
rules: {
|
||||
canDrag: () => true,
|
||||
|
||||
@@ -4,6 +4,7 @@ import {
|
||||
BG_COLORS,
|
||||
RADIUS_PRESETS,
|
||||
SPACING_PRESETS,
|
||||
SHADOW_PRESETS,
|
||||
} from '../../../constants/presets';
|
||||
import {
|
||||
StylePanelProps,
|
||||
@@ -11,16 +12,49 @@ import {
|
||||
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<StylePanelProps> = ({ selectedId, nodeProps }) => {
|
||||
const { actions } = useEditor();
|
||||
const style: CSSProperties = nodeProps.style || {};
|
||||
|
||||
const { setPropStyle } = useNodeProp(selectedId);
|
||||
const { setProp, setPropStyle } = useNodeProp(selectedId);
|
||||
|
||||
const setButtonColor = useCallback(
|
||||
(bgColor: string) => {
|
||||
@@ -61,6 +95,16 @@ export const ButtonStylePanel: React.FC<StylePanelProps> = ({ selectedId, nodePr
|
||||
actions.setProp(selectedId, (props: any) => { props.href = v; });
|
||||
}}
|
||||
/>
|
||||
<div className="guided-section">
|
||||
<label style={{ display: 'flex', alignItems: 'center', gap: 6, fontSize: 11, color: '#e4e4e7', cursor: 'pointer' }}>
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={nodeProps.target === '_blank'}
|
||||
onChange={(e) => setProp('target', e.target.checked ? '_blank' : '_self')}
|
||||
/>
|
||||
Open in new tab
|
||||
</label>
|
||||
</div>
|
||||
<div className="guided-section">
|
||||
<SectionLabel>Border Radius</SectionLabel>
|
||||
<PresetButtonGrid
|
||||
@@ -77,6 +121,70 @@ export const ButtonStylePanel: React.FC<StylePanelProps> = ({ selectedId, nodePr
|
||||
onSelect={(v) => setPropStyle('padding', v)}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* Hover state -- rendered into a scoped <style>...:hover{} block by
|
||||
ButtonLink.toHtml (published export only; not shown live in the
|
||||
editor canvas beyond the hover preview ButtonLink itself does). */}
|
||||
<CollapsibleSection title="Hover State" defaultOpen={false}>
|
||||
<ColorPickerField
|
||||
label="Hover Background"
|
||||
value={nodeProps.hoverBg || ''}
|
||||
onChange={(v) => setProp('hoverBg', v)}
|
||||
/>
|
||||
<ColorPickerField
|
||||
label="Hover Text Color"
|
||||
value={nodeProps.hoverColor || ''}
|
||||
onChange={(v) => setProp('hoverColor', v)}
|
||||
/>
|
||||
</CollapsibleSection>
|
||||
|
||||
{/* Box model + border/effects + animation/visibility rollout */}
|
||||
<CollapsibleSection title="Spacing" defaultOpen={false}>
|
||||
<SpacingControl
|
||||
label="Margin"
|
||||
value={{ top: style.marginTop as string, right: style.marginRight as string, bottom: style.marginBottom as string, left: style.marginLeft as string }}
|
||||
onChange={(side: SpacingSide, v: string) => setPropStyle(`margin${capitalize(side)}`, v)}
|
||||
/>
|
||||
</CollapsibleSection>
|
||||
<CollapsibleSection title="Border & Effects" defaultOpen={false}>
|
||||
<BorderControl
|
||||
value={parseBorderShorthand(style.border as string)}
|
||||
onChange={(v) => setPropStyle('border', buildBorderShorthand(v))}
|
||||
/>
|
||||
<div className="guided-section">
|
||||
<SectionLabel>Shadow</SectionLabel>
|
||||
<PresetButtonGrid presets={SHADOW_PRESETS} activeValue={style.boxShadow as string} onSelect={(v) => setPropStyle('boxShadow', v)} />
|
||||
</div>
|
||||
<div style={sectionGap}>
|
||||
<label style={labelStyle}>Opacity: {opacityPercent(style.opacity)}%</label>
|
||||
<input
|
||||
type="range"
|
||||
min={0}
|
||||
max={100}
|
||||
value={opacityPercent(style.opacity)}
|
||||
onChange={(e) => setPropStyle('opacity', String(Number(e.target.value) / 100))}
|
||||
style={{ width: '100%' }}
|
||||
/>
|
||||
</div>
|
||||
</CollapsibleSection>
|
||||
<CollapsibleSection title="Animation & Visibility" defaultOpen={false}>
|
||||
<AnimationControl
|
||||
value={{ animation: nodeProps.animation || 'none', animationDelay: nodeProps.animationDelay }}
|
||||
onChange={(v) => { setProp('animation', v.animation); setProp('animationDelay', v.animationDelay); }}
|
||||
/>
|
||||
<VisibilityControl
|
||||
value={{
|
||||
hideOnDesktop: nodeProps.hideOnDesktop,
|
||||
hideOnTablet: nodeProps.hideOnTablet,
|
||||
hideOnMobile: nodeProps.hideOnMobile,
|
||||
}}
|
||||
onChange={(v) => {
|
||||
setProp('hideOnDesktop', !!v.hideOnDesktop);
|
||||
setProp('hideOnTablet', !!v.hideOnTablet);
|
||||
setProp('hideOnMobile', !!v.hideOnMobile);
|
||||
}}
|
||||
/>
|
||||
</CollapsibleSection>
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
@@ -4,20 +4,69 @@ import {
|
||||
FONT_FAMILIES,
|
||||
TEXT_SIZES,
|
||||
FONT_WEIGHTS,
|
||||
LINE_HEIGHTS,
|
||||
LETTER_SPACINGS,
|
||||
SHADOW_PRESETS,
|
||||
} from '../../../constants/presets';
|
||||
import {
|
||||
StylePanelProps,
|
||||
SectionLabel,
|
||||
ColorSwatchGrid,
|
||||
PresetButtonGrid,
|
||||
NumericUnitInput,
|
||||
CollapsibleSection,
|
||||
SpacingControl,
|
||||
SpacingSide,
|
||||
BorderControl,
|
||||
BorderValue,
|
||||
buildBorderShorthand,
|
||||
AnimationControl,
|
||||
VisibilityControl,
|
||||
sectionGap,
|
||||
labelStyle,
|
||||
useNodeProp,
|
||||
} from './shared';
|
||||
|
||||
/* Text-transform is a small fixed enum with no natural home in the shared
|
||||
foundation presets (constants/presets.ts is import-only for this
|
||||
package), so it lives here as a package-local preset list. */
|
||||
const TEXT_TRANSFORMS: { label: string; value: string }[] = [
|
||||
{ label: 'None', value: 'none' },
|
||||
{ label: 'UPPER', value: 'uppercase' },
|
||||
{ label: 'lower', value: 'lowercase' },
|
||||
{ label: 'Capitalize', value: 'capitalize' },
|
||||
];
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/* ---------- TEXT ---------- */
|
||||
export const TextStylePanel: React.FC<StylePanelProps> = ({ selectedId, nodeProps }) => {
|
||||
const style: CSSProperties = nodeProps.style || {};
|
||||
|
||||
const { setPropStyle } = useNodeProp(selectedId);
|
||||
const { setProp, setPropStyle } = useNodeProp(selectedId);
|
||||
|
||||
const isItalic = style.fontStyle === 'italic';
|
||||
const isUnderline = style.textDecoration === 'underline';
|
||||
|
||||
return (
|
||||
<>
|
||||
@@ -44,6 +93,15 @@ export const TextStylePanel: React.FC<StylePanelProps> = ({ selectedId, nodeProp
|
||||
activeValue={style.fontSize as string}
|
||||
onSelect={(v) => setPropStyle('fontSize', v)}
|
||||
/>
|
||||
<div style={{ marginTop: 6 }}>
|
||||
<NumericUnitInput
|
||||
value={TEXT_SIZES.some((p) => p.value === style.fontSize) ? '' : ((style.fontSize as string) || '')}
|
||||
onChange={(v) => setPropStyle('fontSize', v)}
|
||||
units={['px', 'em', 'rem', '%']}
|
||||
placeholder="custom"
|
||||
testId="text-fontsize-custom"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<div className="guided-section">
|
||||
<SectionLabel>Font Weight</SectionLabel>
|
||||
@@ -53,6 +111,53 @@ export const TextStylePanel: React.FC<StylePanelProps> = ({ selectedId, nodeProp
|
||||
onSelect={(v) => setPropStyle('fontWeight', v)}
|
||||
/>
|
||||
</div>
|
||||
<div className="guided-section">
|
||||
<SectionLabel>Style</SectionLabel>
|
||||
<div style={{ display: 'flex', gap: 6 }}>
|
||||
<button
|
||||
type="button"
|
||||
className={`preset-btn ${isItalic ? 'active' : ''}`}
|
||||
style={{ flex: 1, fontStyle: 'italic' }}
|
||||
onClick={() => setPropStyle('fontStyle', isItalic ? 'normal' : 'italic')}
|
||||
title="Italic"
|
||||
>
|
||||
<i className="fa fa-italic" /> Italic
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
className={`preset-btn ${isUnderline ? 'active' : ''}`}
|
||||
style={{ flex: 1, textDecoration: 'underline' }}
|
||||
onClick={() => setPropStyle('textDecoration', isUnderline ? 'none' : 'underline')}
|
||||
title="Underline"
|
||||
>
|
||||
<i className="fa fa-underline" /> Underline
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<div className="guided-section">
|
||||
<SectionLabel>Text Transform</SectionLabel>
|
||||
<PresetButtonGrid
|
||||
presets={TEXT_TRANSFORMS}
|
||||
activeValue={(style.textTransform as string) || 'none'}
|
||||
onSelect={(v) => setPropStyle('textTransform', v === 'none' ? '' : v)}
|
||||
/>
|
||||
</div>
|
||||
<div className="guided-section">
|
||||
<SectionLabel>Line Height</SectionLabel>
|
||||
<PresetButtonGrid
|
||||
presets={LINE_HEIGHTS}
|
||||
activeValue={String(style.lineHeight || '')}
|
||||
onSelect={(v) => setPropStyle('lineHeight', v)}
|
||||
/>
|
||||
</div>
|
||||
<div className="guided-section">
|
||||
<SectionLabel>Letter Spacing</SectionLabel>
|
||||
<PresetButtonGrid
|
||||
presets={LETTER_SPACINGS}
|
||||
activeValue={String(style.letterSpacing || '')}
|
||||
onSelect={(v) => setPropStyle('letterSpacing', v)}
|
||||
/>
|
||||
</div>
|
||||
<div className="guided-section">
|
||||
<SectionLabel>Alignment</SectionLabel>
|
||||
<div className="preset-grid align-grid">
|
||||
@@ -68,6 +173,59 @@ export const TextStylePanel: React.FC<StylePanelProps> = ({ selectedId, nodeProp
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Box model + border/effects + animation/visibility rollout */}
|
||||
<CollapsibleSection title="Spacing" defaultOpen={false}>
|
||||
<SpacingControl
|
||||
label="Margin"
|
||||
value={{ top: style.marginTop as string, right: style.marginRight as string, bottom: style.marginBottom as string, left: style.marginLeft as string }}
|
||||
onChange={(side: SpacingSide, v: string) => setPropStyle(`margin${capitalize(side)}`, v)}
|
||||
/>
|
||||
<SpacingControl
|
||||
label="Padding"
|
||||
value={{ top: style.paddingTop as string, right: style.paddingRight as string, bottom: style.paddingBottom as string, left: style.paddingLeft as string }}
|
||||
onChange={(side: SpacingSide, v: string) => setPropStyle(`padding${capitalize(side)}`, v)}
|
||||
/>
|
||||
</CollapsibleSection>
|
||||
<CollapsibleSection title="Border & Effects" defaultOpen={false}>
|
||||
<BorderControl
|
||||
value={parseBorderShorthand(style.border as string)}
|
||||
onChange={(v) => setPropStyle('border', buildBorderShorthand(v))}
|
||||
/>
|
||||
<div className="guided-section">
|
||||
<SectionLabel>Shadow</SectionLabel>
|
||||
<PresetButtonGrid presets={SHADOW_PRESETS} activeValue={style.boxShadow as string} onSelect={(v) => setPropStyle('boxShadow', v)} />
|
||||
</div>
|
||||
<div style={sectionGap}>
|
||||
<label style={labelStyle}>Opacity: {opacityPercent(style.opacity)}%</label>
|
||||
<input
|
||||
type="range"
|
||||
min={0}
|
||||
max={100}
|
||||
value={opacityPercent(style.opacity)}
|
||||
onChange={(e) => setPropStyle('opacity', String(Number(e.target.value) / 100))}
|
||||
style={{ width: '100%' }}
|
||||
/>
|
||||
</div>
|
||||
</CollapsibleSection>
|
||||
<CollapsibleSection title="Animation & Visibility" defaultOpen={false}>
|
||||
<AnimationControl
|
||||
value={{ animation: nodeProps.animation || 'none', animationDelay: nodeProps.animationDelay }}
|
||||
onChange={(v) => { setProp('animation', v.animation); setProp('animationDelay', v.animationDelay); }}
|
||||
/>
|
||||
<VisibilityControl
|
||||
value={{
|
||||
hideOnDesktop: nodeProps.hideOnDesktop,
|
||||
hideOnTablet: nodeProps.hideOnTablet,
|
||||
hideOnMobile: nodeProps.hideOnMobile,
|
||||
}}
|
||||
onChange={(v) => {
|
||||
setProp('hideOnDesktop', !!v.hideOnDesktop);
|
||||
setProp('hideOnTablet', !!v.hideOnTablet);
|
||||
setProp('hideOnMobile', !!v.hideOnMobile);
|
||||
}}
|
||||
/>
|
||||
</CollapsibleSection>
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user