Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(richtext): give each list depth its own marker, so the second bulleted level draws circles (#7024)

Cindy Zhang8aa0e945e94f
diff --git a/packages/richtext/src/__tests__/MarkdownParitySandbox.a11y.chromium.spec.ts b/packages/richtext/src/__tests__/MarkdownParitySandbox.a11y.chromium.spec.ts
index 4ead436dbe..dbd5dd9609 100644
--- a/packages/richtext/src/__tests__/MarkdownParitySandbox.a11y.chromium.spec.ts
+++ b/packages/richtext/src/__tests__/MarkdownParitySandbox.a11y.chromium.spec.ts
@@ -812,6 +812,102 @@ for (const globals of [
});
}
+// spec:AST-061 DEC-6: a list nested inside n lists, of either kind, draws
+// disc, circle, or square, or writes decimal, lower-alpha, or lower-roman
+// numbers, for n modulo 3 = 0, 1, 2, in the editor and the view alike.
+const BULLET_MARKERS = ['disc', 'circle', 'square'];
+const NUMBER_MARKERS = ['decimal', 'lower-alpha', 'lower-roman'];
+
+for (const globals of [
+ 'colorMode:light;direction:ltr',
+ 'colorMode:dark;direction:rtl',
+]) {
+ test(`list markers cycle by depth (${globals})`, async ({page}) => {
+ await page.setViewportSize(DESKTOP);
+ await page.goto(
+ `${storybook.origin}/iframe.html?id=lab-richtexteditor--markdown-serializers&viewMode=story&globals=astryxTheme:neutral;${globals}`,
+ {waitUntil: 'load'},
+ );
+ const depths = Array.from({length: 9}, (_, depth) => depth);
+ const bullets = depths
+ .map(depth => `${' '.repeat(depth)}- bullet ${depth}`)
+ .join('\n');
+ const numbers = depths
+ .map(depth => `${' '.repeat(depth)}1. number ${depth}`)
+ .join('\n');
+ const mixed = [
+ '- mixed 0',
+ ' 1. mixed 1',
+ ' - mixed 2',
+ ' 1. mixed 3',
+ ].join('\n');
+ await page.locator('textarea').fill([bullets, numbers, mixed].join('\n\n'));
+ await expect(page.getByText('mixed 3', {exact: true})).toHaveCount(2);
+ const surfaces = await page.evaluate(() =>
+ [...document.querySelectorAll<HTMLElement>('[data-lexical-editor]')]
+ // The story keeps some sections hidden until a test shows them.
+ .filter(root => root.checkVisibility())
+ .map(root =>
+ [...root.querySelectorAll('ul, ol')].map(list => {
+ let depth = 0;
+ for (
+ let ancestor = list.parentElement;
+ ancestor != null && ancestor !== root;
+ ancestor = ancestor.parentElement
+ ) {
+ if (ancestor.tagName === 'UL' || ancestor.tagName === 'OL') {
+ depth++;
+ }
+ }
+ return {
+ tag: list.tagName,
+ depth,
+ marker: getComputedStyle(list).listStyleType,
+ // The list's own first item's text; none when that item only
+ // holds a nested list.
+ text: (() => {
+ const first = list.querySelector(':scope > li');
+ const only =
+ first?.children.length === 1 ? first.children[0] : null;
+ return only != null && /^(UL|OL)$/.test(only.tagName)
+ ? ''
+ : (first?.textContent ?? '');
+ })(),
+ };
+ }),
+ ),
+ );
+ expect(surfaces).toHaveLength(2);
+ for (const lists of surfaces) {
+ for (const list of lists) {
+ const markers = list.tag === 'UL' ? BULLET_MARKERS : NUMBER_MARKERS;
+ expect(list.marker, `${list.tag} at depth ${list.depth}`).toBe(
+ markers[list.depth % 3],
+ );
+ }
+ // Every depth from 0 to 8 of each kind, and the mixed nesting.
+ for (const tag of ['UL', 'OL']) {
+ expect(
+ [
+ ...new Set(
+ lists
+ .filter(
+ list => list.tag === tag && !list.text.startsWith('mixed'),
+ )
+ .map(list => list.depth),
+ ),
+ ].sort((a, b) => a - b),
+ ).toEqual(depths);
+ }
+ // Depth counts lists of either kind.
+ const mixed = (text: string) => lists.find(list => list.text === text);
+ expect(mixed('mixed 1')?.marker).toBe('lower-alpha');
+ expect(mixed('mixed 2')?.marker).toBe('square');
+ expect(mixed('mixed 3')?.marker).toBe('decimal');
+ }
+ });
+}
+
for (const viewport of [PHONE, {width: 1280, height: 900}] as const) {
for (const globals of [
'colorMode:light;direction:ltr',
diff --git a/packages/richtext/src/editorTheme.ts b/packages/richtext/src/editorTheme.ts
index 85dd04f47b..6238bc67bb 100644
--- a/packages/richtext/src/editorTheme.ts
+++ b/packages/richtext/src/editorTheme.ts
@@ -198,7 +198,6 @@ const editorTheme = stylex.create({
},
maxWidth: PROSE_MEASURE,
paddingInlineStart: spacingVars['--spacing-6'],
- listStyleType: 'disc',
listStylePosition: 'outside',
},
ol: {
@@ -212,38 +211,21 @@ const editorTheme = stylex.create({
},
maxWidth: PROSE_MEASURE,
paddingInlineStart: spacingVars['--spacing-6'],
- listStyleType: 'decimal',
listStylePosition: 'outside',
},
- // Nested lists: no extra vertical margin, and cycle marker styles per depth
- // to match native browser list nesting. Lexical indexes ulDepth/olDepth by
- // `depth % array.length`, so three entries give three distinct levels that
- // then repeat — matching the browser default disc → circle → square cycle.
- ulNested: {
- marginBlockStart: 0,
- marginBlockEnd: 0,
- },
- ulDepth2: {
- listStyleType: 'circle',
- },
- ulDepth3: {
- listStyleType: 'square',
- },
- olNested: {
- marginBlockStart: 0,
- marginBlockEnd: 0,
- },
- olDepth2: {
- listStyleType: 'lower-alpha',
- },
- olDepth3: {
- listStyleType: 'lower-roman',
- },
- // Lexical's own checkbox lists (`<ul __lexicallisttype="check">`): every
- // item is a task, laid out like the task items of a bulleted list below.
- checklist: {
- listStyleType: 'none',
- },
+ // Markers by depth (spec:AST-061 DEC-6): a list nested inside n lists, of
+ // either kind, draws disc, circle, or square, or writes decimal,
+ // lower-alpha, or lower-roman numbers, for n modulo 3 = 0, 1, 2. Lexical
+ // gives every list its tag's class above and the depth class at
+ // `depth % 3`; only the depth class names a marker, so no two classes on a
+ // list compete for it. A nested list sits alone in its item, so the first-
+ // and last-child rules above already take its margins away.
+ ulMarker0: {listStyleType: 'disc'},
+ ulMarker1: {listStyleType: 'circle'},
+ ulMarker2: {listStyleType: 'square'},
+ olMarker0: {listStyleType: 'decimal'},
+ olMarker1: {listStyleType: 'lower-alpha'},
+ olMarker2: {listStyleType: 'lower-roman'},
// List items space like core Markdown's compact list rows: 4px padding and
// a 2px gap between rows. Lexical puts a nested list in an item of its own
// after its parent item; that wrapper adds no top padding and no marker,
@@ -424,28 +406,8 @@ export function sharedEditorTheme(): EditorThemeClasses {
// A class for a block Lexical may mark `dir="auto"`.
const block = (...styles: Array<stylex.StyleXStyles>): string =>
stylex.props(...styles, editorTheme.providerDirection).className ?? '';
- const ulClass = block(editorTheme.ul);
- const olClass = block(editorTheme.ol);
- const ulDepth2Class = block(
- editorTheme.ul,
- editorTheme.ulNested,
- editorTheme.ulDepth2,
- );
- const ulDepth3Class = block(
- editorTheme.ul,
- editorTheme.ulNested,
- editorTheme.ulDepth3,
- );
- const olDepth2Class = block(
- editorTheme.ol,
- editorTheme.olNested,
- editorTheme.olDepth2,
- );
- const olDepth3Class = block(
- editorTheme.ol,
- editorTheme.olNested,
- editorTheme.olDepth3,
- );
+ const className = (style: stylex.StyleXStyles): string =>
+ stylex.props(style).className ?? '';
return {
paragraph: block(editorTheme.paragraph),
heading: {
@@ -458,17 +420,23 @@ export function sharedEditorTheme(): EditorThemeClasses {
},
quote: block(editorTheme.quote),
list: {
- ul: ulClass,
- ol: olClass,
- checklist: block(editorTheme.ul, editorTheme.checklist),
+ ul: block(editorTheme.ul),
+ ol: block(editorTheme.ol),
listitem: stylex.props(editorTheme.listItem).className,
nested: {
listitem: stylex.props(editorTheme.listItem).className,
},
- // Depth arrays cycle via `depth % length`, so three entries give three
- // distinct nesting levels before repeating (disc→circle→square, etc.).
- ulDepth: [ulClass, ulDepth2Class, ulDepth3Class],
- olDepth: [olClass, olDepth2Class, olDepth3Class],
+ // Lexical picks the class at `depth % 3` (spec:AST-061 DEC-6).
+ ulDepth: [
+ className(editorTheme.ulMarker0),
+ className(editorTheme.ulMarker1),
+ className(editorTheme.ulMarker2),
+ ],
+ olDepth: [
+ className(editorTheme.olMarker0),
+ className(editorTheme.olMarker1),
+ className(editorTheme.olMarker2),
+ ],
},
link: stylex.props(editorTheme.link).className,
text: {