Skip to content

Commit

Permalink
Refactor <Steps> counter (#2041)
Browse files Browse the repository at this point in the history
Co-authored-by: Chris Swithinbank <swithinbank@gmail.com>
  • Loading branch information
HiDeoo and delucis authored Jun 20, 2024
1 parent 53f4cd4 commit 8af5a60
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/strong-apes-burn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/starlight': patch
---

Fixes `<Steps>` numbering bug caused by Chrome v126 CSS counter rewrite
13 changes: 12 additions & 1 deletion packages/starlight/__tests__/remark-rehype/rehype-steps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ test('applies `role="list"` to child list', () => {
test('does not interfere with other attributes on the child list', () => {
const { html } = processSteps('<ol start="5"><li>Step one</li></ol>');
expect(html).toMatchInlineSnapshot(
`"<ol start="5" role="list" class="sl-steps"><li>Step one</li></ol>"`
`"<ol start="5" role="list" class="sl-steps" style="--sl-steps-start: 4"><li>Step one</li></ol>"`
);
});

Expand All @@ -87,3 +87,14 @@ test('applies class name and preserves existing classes on a child list', () =>
`"<ol class="test class-concat sl-steps" role="list"><li>Step one</li></ol>"`
);
});

test('applies custom property if start attribute is used', () => {
const start = 10;
const { html } = processSteps(`<ol start="${start}"><li>Step one</li></ol>`);
expect(html).toContain(`style="--sl-steps-start: ${start - 1}"`);
});

test('custom property for start count does not interfere with custom styles', () => {
const { html } = processSteps(`<ol start="20" style="color: red"><li>Step one</li></ol>`);
expect(html).toMatchInlineSnapshot(`"<ol start="20" style="--sl-steps-start: 19;color: red" role="list" class="sl-steps"><li>Step one</li></ol>"`);
});
4 changes: 3 additions & 1 deletion packages/starlight/user-components/Steps.astro
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ const { html } = processSteps(content);
--bullet-margin: 0.375rem;

list-style: none;
counter-reset: steps-counter var(--sl-steps-start, 0);
padding-inline-start: 0;
}

.sl-steps > li {
counter-increment: steps-counter;
position: relative;
padding-inline-start: calc(var(--bullet-size) + 1rem);
/* HACK: Keeps any `margin-bottom` inside the `<li>`’s padding box to avoid gaps in the hairline border. */
Expand All @@ -31,7 +33,7 @@ const { html } = processSteps(content);

/* Custom list marker element. */
.sl-steps > li::before {
content: counter(list-item);
content: counter(steps-counter);
position: absolute;
top: 0;
inset-inline-start: 0;
Expand Down
8 changes: 8 additions & 0 deletions packages/starlight/user-components/rehype-steps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ const stepsProcessor = rehype()
} else {
rootElement.properties.className.push('sl-steps');
}

// Add the `start` attribute as a CSS custom property so we can use it as the starting index
// of the steps custom counter.
if (typeof rootElement.properties.start === 'number') {
const styles = [`--sl-steps-start: ${rootElement.properties.start - 1}`];
if (rootElement.properties.style) styles.push(String(rootElement.properties.style));
rootElement.properties.style = styles.join(';');
}
};
});

Expand Down

0 comments on commit 8af5a60

Please sign in to comment.