Skip to content

Commit e0eaf3f

Browse files
builder-factorybuilderio-botsteve8708
authored
Step font size 1px with arrows, 10px with Shift (#8705)
* toolkit: step font size 1px (Shift 10px) with arrow keys * Keep font size arrow * fix(toolkit): commit every font size arrow step * Ignore Alt when stepping font size with arrow keys</title> </commit_message>ETwitter </|im_end|> <commit_message> <title>Ignore Alt when stepping font size with arrow keys * Test font size arrow key increments and Shift steps * Fix relative font-size steps and preserve rich-text ranges * Fix font size arrow steps at bounds and for mixed values * Fix bounded font resizing for mixed rich-text objects * Preserve redo history when font resizing makes no change * Replace non-null assertions in rich text selection tests * Preserve saved text ranges when repeatedly resizing selections * Slides: move font-size focus regression test to jsdom * Remove new prose comments and non-null assertions flagged by guards --------- Co-authored-by: Builder.io <builder-bot@builder.io> Co-authored-by: Steve Sewell <steve@builder.io>
1 parent 6459d77 commit e0eaf3f

11 files changed

Lines changed: 1171 additions & 108 deletions

‎.changeset/font-size-arrow-step.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@agent-native/toolkit": patch
3+
---
4+
5+
The font size field now steps 1px with ArrowUp/ArrowDown and 10px with Shift, instead of jumping between preset sizes.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// @vitest-environment happy-dom
2+
3+
import { cleanup, fireEvent, render, screen } from "@testing-library/react";
4+
import { useState } from "react";
5+
import { afterEach, expect, it } from "vitest";
6+
7+
import { FontSizeField } from "./font-size.js";
8+
9+
afterEach(cleanup);
10+
11+
it("steps font sizes by one pixel, or ten pixels with Shift", () => {
12+
function Controlled() {
13+
const [value, setValue] = useState(40);
14+
return (
15+
<FontSizeField
16+
label="Size"
17+
openLabel="Font sizes"
18+
mixedLabel="Mixed"
19+
value={value}
20+
onChange={setValue}
21+
/>
22+
);
23+
}
24+
render(<Controlled />);
25+
const input = screen.getByLabelText<HTMLInputElement>("Size");
26+
fireEvent.focus(input);
27+
fireEvent.keyDown(input, { key: "ArrowUp" });
28+
expect(input.value).toBe("41");
29+
fireEvent.keyDown(input, { key: "ArrowDown" });
30+
expect(input.value).toBe("40");
31+
fireEvent.keyDown(input, { key: "ArrowUp", shiftKey: true });
32+
expect(input.value).toBe("50");
33+
fireEvent.keyDown(input, { key: "ArrowDown", shiftKey: true });
34+
expect(input.value).toBe("40");
35+
});

‎packages/toolkit/src/design-tweaks/font-size.spec.tsx‎

Lines changed: 278 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,12 @@ describe("FontSizeField", () => {
146146
});
147147

148148
it("opens the scale with Alt+ArrowDown", () => {
149-
const { input } = renderField();
149+
const { input, onChange } = renderField();
150150

151151
fireEvent.keyDown(input, { key: "ArrowDown", altKey: true });
152152

153153
expect(screen.getAllByRole("option")).toHaveLength(SCALE.length);
154+
expect(onChange).not.toHaveBeenCalled();
154155
});
155156

156157
it("commits a picked size and closes the list", () => {
@@ -164,7 +165,7 @@ describe("FontSizeField", () => {
164165
expect(screen.queryAllByRole("option")).toHaveLength(0);
165166
});
166167

167-
it("steps to the next and previous size on the scale with the arrow keys", () => {
168+
it("steps 1px with the arrow keys and 10px with Shift", () => {
168169
function Controlled() {
169170
const [value, setValue] = useState(20);
170171
return (
@@ -183,12 +184,54 @@ describe("FontSizeField", () => {
183184
fireEvent.focus(input);
184185

185186
fireEvent.keyDown(input, { key: "ArrowUp" });
186-
expect(input.value).toBe("24");
187-
fireEvent.keyDown(input, { key: "ArrowUp" });
188-
expect(input.value).toBe("32");
187+
expect(input.value).toBe("21");
188+
fireEvent.keyDown(input, { key: "ArrowUp", shiftKey: true });
189+
expect(input.value).toBe("31");
189190
fireEvent.keyDown(input, { key: "ArrowDown" });
191+
expect(input.value).toBe("30");
192+
fireEvent.keyDown(input, { key: "ArrowDown", shiftKey: true });
193+
expect(input.value).toBe("20");
194+
fireEvent.keyDown(input, { key: "ArrowUp", altKey: true });
195+
expect(input.value).toBe("21");
196+
fireEvent.keyDown(input, { key: "ArrowUp", altKey: true, shiftKey: true });
197+
expect(input.value).toBe("31");
198+
});
199+
200+
it("commits every arrow step while the parent value is delayed", () => {
201+
const { input, onChange } = renderField({ value: 40 });
202+
fireEvent.focus(input);
203+
204+
fireEvent.keyDown(input, { key: "ArrowDown", shiftKey: true });
205+
fireEvent.keyDown(input, { key: "ArrowUp" });
190206
fireEvent.keyDown(input, { key: "ArrowDown" });
191-
expect(input.value).toBe("16");
207+
fireEvent.keyDown(input, { key: "ArrowUp", shiftKey: true });
208+
fireEvent.keyDown(input, { key: "ArrowDown", shiftKey: true });
209+
210+
expect(input.value).toBe("30");
211+
expect(onChange.mock.calls).toEqual([
212+
[30, COMMIT],
213+
[31, COMMIT],
214+
[30, COMMIT],
215+
[40, COMMIT],
216+
[30, COMMIT],
217+
]);
218+
});
219+
220+
it("clamps arrow steps to bounds and rejects non-positive results", () => {
221+
const { input, onChange } = renderField({ value: 24, min: 8, max: 30 });
222+
fireEvent.focus(input);
223+
fireEvent.keyDown(input, { key: "ArrowUp", shiftKey: true });
224+
expect(onChange).toHaveBeenLastCalledWith(30, COMMIT);
225+
226+
fireEvent.change(input, { target: { value: "9" } });
227+
fireEvent.keyDown(input, { key: "ArrowDown", shiftKey: true });
228+
expect(onChange).toHaveBeenLastCalledWith(8, COMMIT);
229+
cleanup();
230+
231+
const unbounded = renderField({ value: 5, min: undefined });
232+
fireEvent.focus(unbounded.input);
233+
fireEvent.keyDown(unbounded.input, { key: "ArrowDown", shiftKey: true });
234+
expect(unbounded.onChange).not.toHaveBeenCalled();
192235
});
193236

194237
it("shows the mixed label and applies a picked size to all", () => {
@@ -413,6 +456,233 @@ describe("FontSizeField mixed relative expressions", () => {
413456
});
414457
});
415458

459+
describe("font size arrow regressions", () => {
460+
afterEach(cleanup);
461+
462+
it("commits repeated mixed arrow steps as relative expressions even at representative bounds", () => {
463+
const onChange = vi.fn();
464+
const { rerender } = render(
465+
<FontSizeField
466+
label="Size"
467+
openLabel="Font sizes"
468+
mixedLabel="Valores mixtos"
469+
mixed
470+
allowRelativeExpressions
471+
value={30}
472+
min={8}
473+
max={30}
474+
unit="px"
475+
onChange={onChange}
476+
/>,
477+
);
478+
const input = screen.getByLabelText<HTMLInputElement>("Size");
479+
fireEvent.focus(input);
480+
fireEvent.keyDown(input, { key: "ArrowUp" });
481+
fireEvent.keyDown(input, { key: "ArrowUp" });
482+
fireEvent.keyDown(input, { key: "ArrowUp", shiftKey: true });
483+
expect(input.value).toBe("Valores mixtos");
484+
rerender(
485+
<FontSizeField
486+
label="Size"
487+
openLabel="Font sizes"
488+
mixedLabel="Valores mixtos"
489+
mixed
490+
allowRelativeExpressions
491+
value={8}
492+
min={8}
493+
max={30}
494+
unit="px"
495+
onChange={onChange}
496+
/>,
497+
);
498+
fireEvent.keyDown(input, { key: "ArrowDown" });
499+
fireEvent.keyDown(input, { key: "ArrowDown", shiftKey: true });
500+
fireEvent.blur(input);
501+
expect(input.value).toBe("Valores mixtos");
502+
const meta = (expression: string) => ({
503+
phase: "commit",
504+
relativeExpression: {
505+
expression,
506+
min: 8,
507+
max: 30,
508+
unit: "px",
509+
precision: 1,
510+
},
511+
});
512+
expect(onChange.mock.calls).toEqual([
513+
[30, meta("Mixed+1")],
514+
[30, meta("Mixed+1")],
515+
[30, meta("Mixed+10")],
516+
[8, meta("Mixed-1")],
517+
[8, meta("Mixed-10")],
518+
]);
519+
});
520+
521+
it("keeps absolute stepping for mixed selections without relative expressions and for typed numbers", () => {
522+
const onChange = vi.fn();
523+
const { rerender } = render(
524+
<FontSizeField
525+
label="Size"
526+
openLabel="Font sizes"
527+
mixedLabel="Mixed"
528+
mixed
529+
value={20}
530+
onChange={onChange}
531+
/>,
532+
);
533+
const input = screen.getByLabelText<HTMLInputElement>("Size");
534+
fireEvent.focus(input);
535+
fireEvent.keyDown(input, { key: "ArrowUp" });
536+
expect(onChange).toHaveBeenLastCalledWith(21, COMMIT);
537+
rerender(
538+
<FontSizeField
539+
label="Size"
540+
openLabel="Font sizes"
541+
mixedLabel="Mixed"
542+
mixed
543+
allowRelativeExpressions
544+
value={20}
545+
onChange={onChange}
546+
/>,
547+
);
548+
fireEvent.change(input, { target: { value: "25" } });
549+
fireEvent.keyDown(input, { key: "ArrowDown" });
550+
expect(onChange).toHaveBeenLastCalledWith(24, COMMIT);
551+
});
552+
553+
it.each([
554+
{ value: 40, outward: "ArrowUp" },
555+
{ value: 5, outward: "ArrowDown" },
556+
])(
557+
"does not reverse a mixed absolute step from $value",
558+
({ value, outward }) => {
559+
const { onChange, input } = renderField({
560+
mixed: true,
561+
value,
562+
min: 8,
563+
max: 30,
564+
});
565+
fireEvent.focus(input);
566+
fireEvent.keyDown(input, { key: outward });
567+
expect(onChange).not.toHaveBeenCalled();
568+
expect(input.value).toBe("Mixed");
569+
},
570+
);
571+
572+
it.each([
573+
{ value: 40, outward: "ArrowUp", inward: "ArrowDown", expected: 30 },
574+
{ value: 5, outward: "ArrowDown", inward: "ArrowUp", expected: 8 },
575+
])(
576+
"never reverses an arrow step when $value is outside the bounds",
577+
({ value, outward, inward, expected }) => {
578+
const { onChange, input } = renderField({ value, min: 8, max: 30 });
579+
fireEvent.focus(input);
580+
fireEvent.keyDown(input, { key: outward });
581+
expect(onChange).not.toHaveBeenCalled();
582+
fireEvent.keyDown(input, { key: inward });
583+
expect(onChange).toHaveBeenCalledOnce();
584+
expect(onChange).toHaveBeenCalledWith(expected, COMMIT);
585+
},
586+
);
587+
});
588+
589+
describe("FontSizeField mixed relative arrow steps", () => {
590+
afterEach(cleanup);
591+
592+
it("emits each relative step at representative bounds while retaining the mixed label", () => {
593+
const { onChange, input } = renderField({
594+
mixed: true,
595+
mixedLabel: "Valores mixtos",
596+
allowRelativeArrowSteps: true,
597+
value: 30,
598+
min: 8,
599+
max: 30,
600+
});
601+
fireEvent.focus(input);
602+
fireEvent.keyDown(input, { key: "ArrowUp" });
603+
fireEvent.keyDown(input, { key: "ArrowUp" });
604+
fireEvent.keyDown(input, { key: "ArrowUp", shiftKey: true });
605+
fireEvent.keyDown(input, { key: "ArrowDown" });
606+
fireEvent.keyDown(input, { key: "ArrowDown", shiftKey: true });
607+
fireEvent.blur(input);
608+
expect(input.value).toBe("Valores mixtos");
609+
expect(onChange.mock.calls).toEqual([
610+
[30, { phase: "commit", relativeStep: 1 }],
611+
[30, { phase: "commit", relativeStep: 1 }],
612+
[30, { phase: "commit", relativeStep: 10 }],
613+
[29, { phase: "commit", relativeStep: -1 }],
614+
[20, { phase: "commit", relativeStep: -10 }],
615+
]);
616+
cleanup();
617+
618+
const lower = renderField({
619+
mixed: true,
620+
allowRelativeArrowSteps: true,
621+
value: 8,
622+
min: 8,
623+
max: 30,
624+
});
625+
fireEvent.focus(lower.input);
626+
fireEvent.keyDown(lower.input, { key: "ArrowDown" });
627+
fireEvent.keyDown(lower.input, { key: "ArrowDown", shiftKey: true });
628+
expect(lower.input.value).toBe("Mixed");
629+
expect(lower.onChange.mock.calls).toEqual([
630+
[8, { phase: "commit", relativeStep: -1 }],
631+
[8, { phase: "commit", relativeStep: -10 }],
632+
]);
633+
});
634+
635+
it("does not enable typed mixed expressions and keeps typed numeric steps absolute", () => {
636+
const { onChange, input } = renderField({
637+
mixed: true,
638+
value: 20,
639+
allowRelativeArrowSteps: true,
640+
});
641+
typeAndEnter(input, "Mixed+2");
642+
expect(onChange).not.toHaveBeenCalled();
643+
expect(input.value).toBe("Mixed");
644+
fireEvent.change(input, { target: { value: "25" } });
645+
fireEvent.keyDown(input, { key: "ArrowDown" });
646+
expect(onChange).toHaveBeenCalledWith(24, COMMIT);
647+
});
648+
649+
it("prefers relative steps when both options are enabled, but retains typed expressions", () => {
650+
const { onChange, input } = renderField({
651+
mixed: true,
652+
value: 20,
653+
allowRelativeArrowSteps: true,
654+
allowRelativeExpressions: true,
655+
});
656+
fireEvent.focus(input);
657+
fireEvent.keyDown(input, { key: "ArrowUp" });
658+
expect(onChange).toHaveBeenCalledWith(21, {
659+
phase: "commit",
660+
relativeStep: 1,
661+
});
662+
typeAndEnter(input, "Mixed+2");
663+
expect(onChange).toHaveBeenLastCalledWith(22, {
664+
phase: "commit",
665+
relativeExpression: {
666+
expression: "Mixed+2",
667+
min: 8,
668+
max: 160,
669+
unit: "px",
670+
precision: 1,
671+
},
672+
});
673+
});
674+
675+
it("keeps non-mixed arrow steps absolute when relative steps are enabled", () => {
676+
const { onChange, input } = renderField({
677+
value: 20,
678+
allowRelativeArrowSteps: true,
679+
});
680+
fireEvent.focus(input);
681+
fireEvent.keyDown(input, { key: "ArrowUp" });
682+
expect(onChange).toHaveBeenCalledWith(21, COMMIT);
683+
});
684+
});
685+
416686
describe("font size helpers", () => {
417687
it("parses sizes with units and rejects non-positive values", () => {
418688
expect(parseFontSizeInput("24px", 16, { unit: "px" })).toBe(24);
@@ -427,5 +697,7 @@ describe("font size helpers", () => {
427697
expect(stepFontSize(25, SCALE, -1)).toBe(24);
428698
expect(stepFontSize(48, SCALE, 1)).toBeNull();
429699
expect(stepFontSize(24, SCALE, 1, { max: 30 })).toBeNull();
700+
expect(stepFontSize(24, [48, 16, 32, 32], 1)).toBe(32);
701+
expect(stepFontSize(32, [48, 16, 32, 32], -1, { min: 20 })).toBeNull();
430702
});
431703
});

0 commit comments

Comments
 (0)