fix(controls): preserve editable color values and settings typography
This commit is contained in:
@@ -22,3 +22,10 @@ Checker, круг, pill или кнопку. Это знак результат
|
||||
Лампочка состояния устройства отдельно использует StatusBadge indicator.
|
||||
|
||||
Explanatory paragraphs use `Description`, the same contract as `SettingsCard.description`: inherited font family, regular 400, `--nodedc-font-size-sm`, `--nodedc-text-muted`, line-height 1.4 and zero paragraph margins. Applications must not create feature-specific equivalents.
|
||||
|
||||
Защита компонента: обычный `p` без класса внутри тела `SettingsCard` получает
|
||||
тот же стиль описания, включая вложенные группы. Это страховка для прежних
|
||||
потребителей, а не замена `Description` в новом коде. Статусы ожидания и
|
||||
пояснения вне карточки также используют `Description` с `role="status"`, если
|
||||
нужно объявление для скринридера. Заголовки, значения полей и кнопки сохраняют
|
||||
собственную типографику; глобальный стиль для всех `p` страницы не вводится.
|
||||
|
||||
@@ -674,7 +674,8 @@ textarea.nodedc-field__control {
|
||||
|
||||
.nodedc-settings-card__titles h2,
|
||||
.nodedc-description,
|
||||
.nodedc-settings-card__titles p {
|
||||
.nodedc-settings-card__titles p,
|
||||
.nodedc-settings-card__body :where(p:not([class])) {
|
||||
font-family: inherit;
|
||||
font-weight: 400;
|
||||
margin: 0;
|
||||
@@ -687,7 +688,8 @@ textarea.nodedc-field__control {
|
||||
}
|
||||
|
||||
.nodedc-description,
|
||||
.nodedc-settings-card__titles p {
|
||||
.nodedc-settings-card__titles p,
|
||||
.nodedc-settings-card__body :where(p:not([class])) {
|
||||
font-family: inherit;
|
||||
font-weight: 400;
|
||||
color: var(--nodedc-text-muted);
|
||||
|
||||
@@ -102,6 +102,10 @@ function SingleRangeControl({
|
||||
"--nodedc-range-editor-characters": editorCharacters,
|
||||
} as CSSProperties;
|
||||
|
||||
// External undo/redo owns the controlled value even while the exact editor is focused.
|
||||
// Otherwise its next blur would commit an obsolete draft over the restored value.
|
||||
useLayoutEffect(() => setDraft(editableNumber(value)), [value]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!editing) setDraft(editableNumber(value));
|
||||
}, [editing, value]);
|
||||
@@ -271,7 +275,9 @@ function hsvToHex({ h, s, v }: Hsv): string {
|
||||
return `#${base.map((channel) => Math.round((channel + offset) * 255).toString(16).padStart(2, "0")).join("")}`;
|
||||
}
|
||||
|
||||
export function ColorField({ value, onChange, label = "Цвет", className, disabled = false, ...props }: ColorFieldProps) {
|
||||
export function ColorField({ value, onChange, label = "Цвет", className, disabled = false, onBlur, onKeyDown, ...props }: ColorFieldProps) {
|
||||
const [draft, setDraft] = useState(value);
|
||||
useEffect(() => setDraft(value), [value]);
|
||||
const validColor = hexToHsv(value);
|
||||
const [hsv, setHsv] = useState<Hsv>(() => validColor ?? { h: 330, s: 82, v: 100 });
|
||||
const style = { "--nodedc-color-value": validColor ? hsvToHex(validColor) : hsvToHex(hsv) } as CSSProperties;
|
||||
@@ -283,6 +289,7 @@ export function ColorField({ value, onChange, label = "Цвет", className, dis
|
||||
const commit = (next: Hsv) => {
|
||||
const normalized = { h: clamp(next.h, 0, 360), s: clamp(next.s, 0, 100), v: clamp(next.v, 0, 100) };
|
||||
setHsv(normalized);
|
||||
setDraft(hsvToHex(normalized));
|
||||
onChange(hsvToHex(normalized));
|
||||
};
|
||||
|
||||
@@ -366,12 +373,25 @@ export function ColorField({ value, onChange, label = "Цвет", className, dis
|
||||
</Dropdown>
|
||||
<input
|
||||
className="nodedc-color-field__text"
|
||||
value={value}
|
||||
{...props}
|
||||
value={draft}
|
||||
aria-label={`${label}: HEX`}
|
||||
disabled={disabled}
|
||||
spellCheck={false}
|
||||
onChange={(event) => onChange(event.target.value)}
|
||||
{...props}
|
||||
onChange={(event) => {
|
||||
const text = event.target.value;
|
||||
setDraft(text);
|
||||
if (parseHexColor(text)) onChange(`#${text.trim().replace(/^#/, "").toLowerCase()}`);
|
||||
}}
|
||||
onBlur={(event) => {
|
||||
setDraft(parseHexColor(draft) ? `#${draft.trim().replace(/^#/, "").toLowerCase()}` : value);
|
||||
onBlur?.(event);
|
||||
}}
|
||||
onKeyDown={(event) => {
|
||||
if (event.key === "Escape") { event.preventDefault(); event.stopPropagation(); setDraft(value); }
|
||||
if (event.key === "Enter") { event.preventDefault(); event.currentTarget.blur(); }
|
||||
onKeyDown?.(event);
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
import {readFile} from 'node:fs/promises';
|
||||
import {createElement as h} from 'react';
|
||||
import {renderToStaticMarkup} from 'react-dom/server';
|
||||
import {LoadingRegion,Description,SettingsCard,ComparisonTable} from '../packages/ui-react/dist/index.js';
|
||||
@@ -23,3 +24,15 @@ test('SettingsCard and standalone explanatory paragraphs use one typography comp
|
||||
assert.match(renderToStaticMarkup(h(Description,null,'help')),/class="nodedc-description"/);
|
||||
assert.match(renderToStaticMarkup(h(SettingsCard,{title:'Settings',description:'help'})),/<p class="nodedc-description">help/);
|
||||
});
|
||||
test('legacy card paragraphs and live descriptions share small muted typography, without targeting controls',async()=>{
|
||||
const css=await readFile(new URL('../packages/ui-core/styles.css',import.meta.url),'utf8');
|
||||
const blocks=[...css.matchAll(/([^{}]+)\{([^{}]*)\}/g)].map(([,selectors,body])=>({selectors,body}));
|
||||
const copy=blocks.find(b=>b.selectors.includes('.nodedc-description')&&b.body.includes('font-size:'));
|
||||
assert.ok(copy);
|
||||
assert.match(copy.selectors,/\.nodedc-settings-card__titles p/);
|
||||
assert.match(copy.selectors,/\.nodedc-settings-card__body :where\(p:not\(\[class\]\)\)/);
|
||||
for(const declaration of ['font-family: inherit','font-weight: 400','color: var(--nodedc-text-muted)','font-size: var(--nodedc-font-size-sm)','line-height: 1.4'])assert.ok(copy.body.includes(declaration),declaration);
|
||||
assert.doesNotMatch(copy.selectors,/h[1-6]|button|input|select/);
|
||||
const markup=renderToStaticMarkup(h(Description,{role:'status','aria-live':'polite'},'Waiting for device'));
|
||||
assert.match(markup,/role="status"/);assert.match(markup,/aria-live="polite"/);assert.match(markup,/class="nodedc-description"/);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user