Skip to content

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

TypeScript

fix(ProgressBar): size the target mark through derived vars (#4970)

Cindy Zhangc1d86e6bebe3
diff --git a/.changeset/progressbar-mark-size-derived-vars.md b/.changeset/progressbar-mark-size-derived-vars.md
new file mode 100644
index 0000000000..8e64d05719
--- /dev/null
+++ b/.changeset/progressbar-mark-size-derived-vars.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] ProgressBar: a theme can size the target mark again without `!important`. The mark's `width`/`height` were plain StyleX declarations, so a `progressbar-mark` override only landed where `@layer astryx-theme` outranks the component atomics — in a source-build app that compiles StyleX without `useCSSLayers` the atomics are unlayered and beat every theme rule, leaving no way to resize the tick but an unlayered `!important` rule. The dimensions now travel as derived vars with no competing declaration, so the same `defineTheme` entry lands in either build. Theme authoring is unchanged; a mark's color is still a plain declaration and still depends on the layer order.
+
+@cixzhang
diff --git a/apps/storybook/stories/ProgressBar.stories.tsx b/apps/storybook/stories/ProgressBar.stories.tsx
index 79a6247a6c..d7b7b48c89 100644
--- a/apps/storybook/stories/ProgressBar.stories.tsx
+++ b/apps/storybook/stories/ProgressBar.stories.tsx
@@ -315,17 +315,20 @@ export const MarksAcrossVariants: Story = {
export const ThemedMarks: Story = {
// Marks are themeable directly via the `progressbar-mark` target: a theme sets
- // `backgroundColor`, `width`, and `height` on it with `defineTheme` — no
- // dedicated CSS vars needed. A taller height overhangs the bar symmetrically
- // above and below. Here we set the properties via a scoped style block so the
- // effect is visible without a full theme.
+ // `backgroundColor`, `width`, and `height` on it with `defineTheme`. A taller
+ // height overhangs the bar symmetrically above and below. The style block below
+ // stands in for a full theme, in the shape `astryx theme build` emits — the
+ // layer, and the size arriving as the derived vars rather than as `width` /
+ // `height` — so the demo exercises the real theming path.
render: () => (
<div style={{width: '320px'}}>
<style>{`
- .themed-marks-demo .astryx-progressbar-mark {
- background-color: red;
- width: 3px;
- height: 14px;
+ @layer astryx-theme {
+ .themed-marks-demo .astryx-progressbar-mark {
+ background-color: red;
+ --_progressbar-mark-width: 3px;
+ --_progressbar-mark-height: 14px;
+ }
}
`}</style>
<div className="themed-marks-demo">
diff --git a/packages/core/src/ProgressBar/ProgressBar.doc.mjs b/packages/core/src/ProgressBar/ProgressBar.doc.mjs
index b1f9659b5d..9018f06d69 100644
--- a/packages/core/src/ProgressBar/ProgressBar.doc.mjs
+++ b/packages/core/src/ProgressBar/ProgressBar.doc.mjs
@@ -85,6 +85,14 @@ export const docs = {
visualProps: ['variant', 'placement'],
},
],
+ vars: [
+ {name: '--_progressbar-mark-width', description: 'Target mark tick width', default: '2px', private: true},
+ {name: '--_progressbar-mark-height', description: 'Target mark tick height', default: '8px', private: true},
+ ],
+ derived: [
+ {property: 'width', vars: ['--_progressbar-mark-width'], replaces: true},
+ {property: 'height', vars: ['--_progressbar-mark-height'], replaces: true},
+ ],
},
usage: {
description:
@@ -182,6 +190,14 @@ export const docsZh = {
visualProps: ['variant', 'placement'],
},
],
+ vars: [
+ {name: '--_progressbar-mark-width', description: '目标标记刻度宽度', default: '2px', private: true},
+ {name: '--_progressbar-mark-height', description: '目标标记刻度高度', default: '8px', private: true},
+ ],
+ derived: [
+ {property: 'width', vars: ['--_progressbar-mark-width'], replaces: true},
+ {property: 'height', vars: ['--_progressbar-mark-height'], replaces: true},
+ ],
},
usage: {
description:
diff --git a/packages/core/src/ProgressBar/ProgressBar.test.tsx b/packages/core/src/ProgressBar/ProgressBar.test.tsx
index aaeea6d7ea..4f68d1155c 100644
--- a/packages/core/src/ProgressBar/ProgressBar.test.tsx
+++ b/packages/core/src/ProgressBar/ProgressBar.test.tsx
@@ -766,9 +766,49 @@ describe('ProgressBar', () => {
expect(trackHasOverflowHidden).toBe(true);
});
+ it('sizes the mark through the derived vars, so a theme override cannot lose the cascade', () => {
+ // width/height are declared as `var(--_progressbar-mark-*)` and nothing
+ // else declares those vars, so the value a theme sets on the
+ // `progressbar-mark` target lands even in a consumer whose StyleX is
+ // unlayered and would otherwise outrank `@layer astryx-theme`.
+ const {container} = render(
+ <ProgressBar
+ value={50}
+ label="Progress"
+ marks={[{value: 80, label: 'M'}]}
+ />,
+ );
+ const mark = container.querySelector<HTMLElement>(MARK)!;
+ let css = '';
+ for (const sheet of Array.from(document.styleSheets)) {
+ try {
+ for (const rule of Array.from(sheet.cssRules)) {
+ css += rule.cssText + '\n';
+ }
+ } catch {
+ // ignore cross-origin sheets
+ }
+ }
+ css += Array.from(document.querySelectorAll('style'))
+ .map(s => s.textContent || '')
+ .join('\n');
+ const markRules = Array.from(mark.classList)
+ .filter(cls => /^x[a-z0-9]+$/.test(cls))
+ .flatMap(
+ cls =>
+ css.match(new RegExp(`\\.${cls}\\b[^{]*\\{[^}]*\\}`, 'g')) ?? [],
+ );
+ expect(markRules.join('\n')).toMatch(
+ /width:\s*var\(--_progressbar-mark-width,\s*2px\)/,
+ );
+ expect(markRules.join('\n')).toMatch(
+ /height:\s*var\(--_progressbar-mark-height,\s*8px\)/,
+ );
+ });
+
it('renders the mark on the stable progressbar-mark target, centered for symmetric overhang', () => {
// The mark's width/height/color are directly overridable via the
- // `progressbar-mark` theme target (no dedicated CSS vars). It is centered
+ // `progressbar-mark` theme target. It is centered
// on the track (translate -50%,-50%) so a themed taller tick overhangs the
// bar symmetrically above and below without being clipped.
const {container} = render(
diff --git a/packages/core/src/ProgressBar/ProgressBar.tsx b/packages/core/src/ProgressBar/ProgressBar.tsx
index fa8bee6d93..78406b3727 100644
--- a/packages/core/src/ProgressBar/ProgressBar.tsx
+++ b/packages/core/src/ProgressBar/ProgressBar.tsx
@@ -261,22 +261,27 @@ const styles = stylex.create({
},
// A mark is a vertical tick centered on the track, a child of the
// `role="progressbar"` element (unchanged DOM). The track no longer clips, so
- // its height — 8px by default, directly overridable via the `progressbar-mark`
- // theme target — may exceed the bar and overhang; the centering translate
- // keeps any overhang symmetric. Positioned horizontally via `insetInlineStart`;
- // the translate mirrors under RTL.
+ // its height — 8px by default — may exceed the bar and overhang; the centering
+ // translate keeps any overhang symmetric. Positioned horizontally via
+ // `insetInlineStart`; the translate mirrors under RTL.
+ //
+ // The dimensions read private vars rather than being plain declarations: a
+ // theme writes `width`/`height` on the `progressbar-mark` target as usual and
+ // the derived-var registry emits them as these vars instead of as competing
+ // properties. Nothing else declares them, so the theme value lands whatever
+ // the consumer's cascade looks like — a source-build app that compiles StyleX
+ // without `useCSSLayers` leaves the atomics unlayered, where they outrank
+ // every rule in `@layer astryx-theme` and made sizing the mark impossible
+ // without `!important`.
//
// The tick's color is not set here: it depends on what the mark sits on, so
// it comes from `markOnFillStyles[variant]` (mark inside the filled area) or
- // `markOnTrackStyles.track` (mark out on the bare track). Both remain
- // directly overridable via the `progressbar-mark` theme target — a theme can
- // set `backgroundColor`, `width`, and `height` (e.g. a taller "flag" tick
- // that overhangs the bar) with `defineTheme`; no dedicated CSS vars needed.
+ // `markOnTrackStyles.track` (mark out on the bare track).
mark: {
position: 'absolute',
top: '50%',
- width: 2,
- height: 8,
+ width: 'var(--_progressbar-mark-width, 2px)',
+ height: 'var(--_progressbar-mark-height, 8px)',
outline: {
default: 'none',
':focus-visible': `2px solid ${colorVars['--color-accent']}`,
diff --git a/packages/core/src/theme/derivedVarRegistry.test.ts b/packages/core/src/theme/derivedVarRegistry.test.ts
index da50250ebc..b43e5dc93b 100644
--- a/packages/core/src/theme/derivedVarRegistry.test.ts
+++ b/packages/core/src/theme/derivedVarRegistry.test.ts
@@ -195,6 +195,7 @@ const DIR_TO_REGISTRY_KEY: Record<string, string> = {
Field: 'field',
HoverCard: 'hovercard',
Popover: 'popover',
+ ProgressBar: 'progressbar-mark',
Section: 'section',
SegmentedControl: 'segmented-control',
TextArea: 'textarea',
@@ -347,4 +348,16 @@ describe('getDerivedVars', () => {
expect(result[0].vars).toEqual(['--_textarea-inline-padding']);
expect(result[0].replaces).toBe(true);
});
+
+ it('marks progressbar-mark width and height as replacing the source property', () => {
+ for (const [property, varName] of [
+ ['width', '--_progressbar-mark-width'],
+ ['height', '--_progressbar-mark-height'],
+ ]) {
+ const result = getDerivedVars('progressbar-mark', property);
+ expect(result).toHaveLength(1);
+ expect(result[0].vars).toEqual([varName]);
+ expect(result[0].replaces).toBe(true);
+ }
+ });
});
diff --git a/packages/core/src/theme/derivedVarRegistry.ts b/packages/core/src/theme/derivedVarRegistry.ts
index 9b977dd2ee..b71a16c575 100644
--- a/packages/core/src/theme/derivedVarRegistry.ts
+++ b/packages/core/src/theme/derivedVarRegistry.ts
@@ -64,6 +64,10 @@ export const derivedVarRegistry: Record<string, DerivedVarEntry[]> = {
field: [{property: 'borderRadius', vars: ['--_field-radius']}],
hovercard: [{property: 'borderRadius', vars: ['--_hovercard-radius']}],
popover: [{property: 'borderRadius', vars: ['--_popover-radius']}],
+ 'progressbar-mark': [
+ {property: 'width', vars: ['--_progressbar-mark-width'], replaces: true},
+ {property: 'height', vars: ['--_progressbar-mark-height'], replaces: true},
+ ],
section: [{property: 'padding', expand: 'container'}],
'segmented-control': [
{property: 'borderRadius', vars: ['--_segmented-control-radius']},
diff --git a/packages/core/src/theme/generateThemeRules.test.ts b/packages/core/src/theme/generateThemeRules.test.ts
index 7e3747a860..4c58efba1e 100644
--- a/packages/core/src/theme/generateThemeRules.test.ts
+++ b/packages/core/src/theme/generateThemeRules.test.ts
@@ -496,6 +496,26 @@ describe('derived var expansion', () => {
// full-bleed textarea and push the native resize grip off the corner.
expect(rule).not.toContain('padding-inline: var(--eps-input-padding-x)');
});
+
+ it('replaces progressbar-mark width/height with vars (no raw properties)', () => {
+ const theme = defineTheme({
+ name: 'test-derived-progressbar-mark',
+ components: {
+ 'progressbar-mark': {
+ base: {width: '2px', height: '12px'},
+ },
+ },
+ });
+ const rules = generateThemeRules(theme);
+ const rule = rules.find(r => r.includes('.astryx-progressbar-mark'));
+ expect(rule).toBeDefined();
+ expect(rule).toContain('--_progressbar-mark-width: 2px');
+ expect(rule).toContain('--_progressbar-mark-height: 12px');
+ // Raw dimensions would be a same-element fight with the mark's StyleX,
+ // which an unlayered consumer build wins — the vars have no competitor.
+ expect(rule).not.toMatch(/[{;]\s*width: 2px/);
+ expect(rule).not.toMatch(/[{;]\s*height: 12px/);
+ });
});
describe('brutalist-style derived expansion', () => {