Repository navigation
Fix layout validators to reject negative percentages in spacing props - #4979
Conversation
…s in spacing props `isPercentage` previously allowed negative percentages (`-?` in the regex), which leaked into `isValidSpacingValue` and `isValidMarginValue`. Negative percentages are not valid spacing values and were being accepted silently. - Remove `-?` from `isPercentage` so spacing/margin/gap props reject negative % - Inline `/^-?\d+(\.\d+)?%$/` in `isValidPositionValue` and `isValidSizeValue` so those continue to accept negative percentages intentionally - Add unit tests covering rejection of negative % for padding, margin, and gap, and the existing passing-through of negative % for position props Co-Authored-By: Claude Sonnet 4.6 (1M context) <[email protected]>
|
Visit https://backpack.github.io/storybook-prs/4979 to see this build running in a browser. |
- Spacing (padding/margin/gap): clarify only non-negative percentages are accepted - Position offsets: explicitly document negative rem and negative % are supported Co-Authored-By: Claude Sonnet 4.6 (1M context) <[email protected]>
|
Visit https://backpack.github.io/storybook-prs/4979 to see this build running in a browser. |
There was a problem hiding this comment.
Pull request overview
Note
Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.
Updates layout prop validators so spacing props reject negative percentage values, while preserving negative percentage support for offset-style props.
Changes:
- Tightens
isPercentageto only accept non-negative percentage strings for spacing validation. - Inlines signed-percentage regex in position/size validators to keep negative percentage support where intended.
- Adds Jest coverage for rejecting negative
%inpadding/margin/gapand allowing it for position props; updates README wording accordingly.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| packages/backpack-web/src/bpk-component-layout/src/tokens.ts | Makes spacing % validation non-negative; inlines signed % regex for position/size validators. |
| packages/backpack-web/src/bpk-component-layout/src/tokenUtils-test.ts | Adds tests ensuring negative % is dropped for spacing props and preserved for position props. |
| packages/backpack-web/src/bpk-component-layout/README.md | Documents that spacing percentages must be non-negative; clarifies negative values for offset props. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| export function isValidSizeValue(value: string): boolean { | ||
| return ( | ||
| /^-?\d+(\.\d+)?rem$/.test(value) || // rem values | ||
| isPercentage(value) || // percentage values | ||
| /^-?\d+(\.\d+)?rem$/.test(value) || // rem values (negative allowed for transforms) | ||
| /^-?\d+(\.\d+)?%$/.test(value) || // percentage values (negative allowed) | ||
| value === 'auto' || | ||
| value === 'full' || | ||
| value === 'fit-content' |
There was a problem hiding this comment.
Fixed in the follow-up commit — isValidSizeValue now uses /^\d+(\.\d+)?rem$/ (no -?) and delegates to isPercentage (non-negative) instead of inlining the signed regex.
| return ( | ||
| /^-?\d+(\.\d+)?rem$/.test(value) || // rem values | ||
| isPercentage(value) || // percentage values | ||
| /^-?\d+(\.\d+)?rem$/.test(value) || // rem values (negative allowed for transforms) |
There was a problem hiding this comment.
Fixed — the misleading comment and the signed regex in isValidSizeValue were both removed in the follow-up commit.
| it('rejects negative percentage for padding and drops the prop', () => { | ||
| const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {}); | ||
|
|
||
| const result = processBpkProps({ | ||
| padding: '-10%' as any, | ||
| paddingTop: '-5%' as any, | ||
| }); | ||
|
|
||
| expect(result.padding).toBeUndefined(); | ||
| expect(result.paddingTop).toBeUndefined(); | ||
| expect(warnSpy).toHaveBeenCalled(); | ||
| warnSpy.mockRestore(); | ||
| }); |
There was a problem hiding this comment.
Fixed — added afterEach(() => jest.restoreAllMocks()) to the processBpkProps describe block so spy cleanup is guaranteed even if an assertion throws.
| */ | ||
| export function isValidSizeValue(value: string): boolean { | ||
| return ( |
There was a problem hiding this comment.
isValidSizeValue comment is misleading (likely a pre-existing bug surfaced here)
/^-?\d+(\.\d+)?rem$/.test(value) || // rem values (negative allowed for transforms)
/^-?\d+(\.\d+)?%$/.test(value) || // percentage values (negative allowed)
isValidSizeValue governs width, height, minWidth, maxWidth, etc. Negative values for these properties are invalid CSS — a negative width or minWidth is meaningless. The comment "negative allowed for transforms" doesn't make sense in this context (transform uses isValidTransformValue, not this function). This looks like a copy-paste from the position validator. Worth either:
- Dropping the -? from both lines here (if negative size was never intentional), or
- Adding a note confirming this is intentional with a concrete use-case.
There was a problem hiding this comment.
Fixed — isValidSizeValue now uses /^\d+(\.\d+)?rem$/ (no -?) and isPercentage (non-negative). Negative size values were never intentional; this was a copy-paste from the position validator.
| /^-?\d+(\.\d+)?rem$/.test(value) || // rem values (negative allowed for transforms) | ||
| /^-?\d+(\.\d+)?%$/.test(value) || // percentage values (negative allowed) |
There was a problem hiding this comment.
The regex /^-?\d+(\.\d+)?%$/ now appears in two places. A named export like:
const NEGATIVE_PERCENTAGE_RE = /^-?\d+(\.\d+)?%$/;
would remove the duplication and make the intent self-documenting. Low priority, but worth doing while the change is open.
There was a problem hiding this comment.
Done — extracted as NEGATIVE_PERCENTAGE_RE and used in isValidPositionValue to eliminate the duplication.
| expect(result.margin).toBeUndefined(); | ||
| expect(result.marginTop).toBeUndefined(); | ||
| expect(warnSpy).toHaveBeenCalled(); | ||
| warnSpy.mockRestore(); | ||
| }); | ||
|
|
||
| it('rejects negative percentage for gap and drops the prop', () => { | ||
| const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {}); | ||
|
|
||
| const result = processBpkProps({ | ||
| gap: '-10%' as any, | ||
| }); | ||
|
|
||
| expect(result.gap).toBeUndefined(); |
There was a problem hiding this comment.
BpkFlex exposes rowGap and columnGap as separate gap props. The new "rejects negative percentage for gap" test only covers gap. If those props go through a different validator path, the fix might not cover them. Worth adding:
const result = processBpkProps({ gap: '-10%', rowGap: '-5%', columnGap: '-5%' } as any);
expect(result.rowGap).toBeUndefined();
expect(result.columnGap).toBeUndefined();
There was a problem hiding this comment.
Done — expanded the gap test to cover rowGap and columnGap as well.
| }); | ||
|
|
||
| expect(result.gap).toBeUndefined(); | ||
| expect(warnSpy).toHaveBeenCalled(); |
There was a problem hiding this comment.
All three new tests assert only expect(warnSpy).toHaveBeenCalled(). If the warning message changes to something uninformative (or is removed), this passes silently. Prefer:
expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining('-10%'));
or at minimum toHaveBeenCalledTimes(1) per rejected prop so the count is explicit.
There was a problem hiding this comment.
Done — assertions now use toHaveBeenCalledWith(expect.stringContaining(value)) so they verify the invalid value appears in the warning message.
| }); | ||
|
|
||
| it('passes through negative percentages for position props (e.g. top: -50% for vertical centering)', () => { | ||
| it('passes through negative percentages for position props (e.g. top: -50% for translate offsets)', () => { |
There was a problem hiding this comment.
There's no test confirming that padding: '50%' still passes after the change. Given this is the exact behavior the fix changes, one positive assertion is cheap insurance.
There was a problem hiding this comment.
Done — added a dedicated test: padding: '50%' passes through as '50%' after the change.
- isValidSizeValue: revert to non-negative rem and percentage (negative size values are not meaningful CSS; the signed regex was introduced unintentionally in the previous commit) - tokenUtils-test: add afterEach(jest.restoreAllMocks) to processBpkProps describe block so warnSpy cannot leak if an assertion throws Co-Authored-By: Claude Sonnet 4.6 (1M context) <[email protected]>
- Extract NEGATIVE_PERCENTAGE_RE constant to avoid duplicating the signed percentage regex in isValidPositionValue - Add rowGap/columnGap coverage to the negative gap test - Tighten warn spy assertions to check the invalid value appears in the message - Add positive padding: '50%' regression guard Co-Authored-By: Claude Sonnet 4.6 (1M context) <[email protected]>
|
Visit https://backpack.github.io/storybook-prs/4979 to see this build running in a browser. |
|
|
||
| // Matches percentage strings that include a leading minus sign, e.g. '-50%', '-0.5%'. | ||
| // Used only by position validators where negative offsets are intentional. | ||
| const NEGATIVE_PERCENTAGE_RE = /^-?\d+(\.\d+)?%$/; |
There was a problem hiding this comment.
how about rename to SIGNED_PERCENTAGE_RE as -? let this regex match both positive and negative percentages simultaneously (both 50% and -50% can pass)
Also comment should update
// Matches both positive and negative percentage strings, e.g. '50%', '-50%', '-0.5%'.
// Used by position validators where negative offsets are intentional.
const SIGNED_PERCENTAGE_RE = /^-?\d+(\.\d+)?%$/;
There was a problem hiding this comment.
Good point — renamed to SIGNED_PERCENTAGE_RE and updated the comment accordingly.
The regex uses -? so it matches both positive and negative percentages. SIGNED_PERCENTAGE_RE is more accurate. Also update the JSDoc reference. Co-Authored-By: Claude Sonnet 4.6 (1M context) <[email protected]>
|
Visit https://backpack.github.io/storybook-prs/4979 to see this build running in a browser. |
Summary
isPercentageintokens.tsused-?in its regex, unintentionally allowing negative percentages to pass validation forpadding,margin, andgappropsisValidPositionValueandisValidSizeValueintentionally support negative values (e.g.top: '-50%'for translate offsets), so they now inline their own/^-?\d+(\.\d+)?%$/regex instead of delegating toisPercentageFiles changed:
tokens.ts— remove-?fromisPercentage; inline negative-% regex inisValidPositionValueandisValidSizeValuetokenUtils-test.ts— add tests asserting negative % is rejected forpadding,margin,gapand accepted for position propsTest plan
pnpm run jest --testPathPattern="tokenUtils-test"— 41 tests passeslinton changed files — no errorspadding/margin/gapis dropped with a console warningtop/left/right/bottomcontinues to pass through unchanged🤖 Generated with Claude Code