Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Box): add shorthand sizes #2048

Merged
merged 2 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions packages/css/box.css
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
@layer fds.box {
.fds-box--xsmall-shadow {
.fds-box--xs-shadow {
box-shadow: var(--fds-shadow-xsmall);
}

.fds-box--small-shadow {
.fds-box--sm-shadow {
box-shadow: var(--fds-shadow-small);
}

.fds-box--medium-shadow {
.fds-box--md-shadow {
box-shadow: var(--fds-shadow-medium);
}

.fds-box--large-shadow {
.fds-box--lg-shadow {
box-shadow: var(--fds-shadow-large);
}

.fds-box--xlarge-shadow {
.fds-box--xl-shadow {
box-shadow: var(--fds-shadow-xlarge);
}

Expand All @@ -31,31 +31,31 @@
border: 1px solid var(--fds-semantic-border-neutral-strong);
}

.fds-box--small-border-radius {
.fds-box--sm-border-radius {
border-radius: var(--fds-border_radius-small);
}

.fds-box--medium-border-radius {
.fds-box--md-border-radius {
border-radius: var(--fds-border_radius-medium);
}

.fds-box--large-border-radius {
.fds-box--lg-border-radius {
border-radius: var(--fds-border_radius-large);
}

.fds-box--xlarge-border-radius {
.fds-box--xl-border-radius {
border-radius: var(--fds-border_radius-xlarge);
}

.fds-box--xxlarge-border-radius {
.fds-box--2xl-border-radius {
border-radius: var(--fds-border_radius-xxlarge);
}

.fds-box--xxxlarge-border-radius {
.fds-box--3xl-border-radius {
border-radius: var(--fds-border_radius-xxxlarge);
}

.fds-box--xxxxlarge-border-radius {
.fds-box--4xl-border-radius {
border-radius: var(--fds-border_radius-xxxxlarge);
}

Expand Down
12 changes: 6 additions & 6 deletions packages/react/src/components/Box/Box.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ const renderAsChild = (props?: BoxProps) =>
);

describe('Box', () => {
it('should render a div with correct classname when shadow is xsmall', () => {
render({ shadow: 'xsmall' });
it('should render a div with correct classname when shadow is xs', () => {
render({ shadow: 'xs' });
const box = screen.getByTitle('box');

expect(box.classList).toContain('fds-box--xsmall-shadow');
expect(box.classList).toContain('fds-box--xs-shadow');
});

it('should render a div with correct classname when borderColor is subtle', () => {
Expand All @@ -38,11 +38,11 @@ describe('Box', () => {
expect(box.classList).toContain('fds-box--subtle-border-color');
});

it('should render a div with correct classname when borderRadius is small', () => {
render({ borderRadius: 'small' });
it('should render a div with correct classname when borderRadius is sm', () => {
render({ borderRadius: 'sm' });
const box = screen.getByTitle('box');

expect(box.classList).toContain('fds-box--small-border-radius');
expect(box.classList).toContain('fds-box--sm-border-radius');
});

it('should render as a button when we use asChild', () => {
Expand Down
37 changes: 26 additions & 11 deletions packages/react/src/components/Box/Box.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,24 @@ import { forwardRef } from 'react';
import cl from 'clsx/lite';
import { Slot } from '@radix-ui/react-slot';

import { getSize } from '../../utilities/getSize';

type OldShadowSizes = 'xsmall' | 'small' | 'medium' | 'large' | 'xlarge';
type OldBorderRadii =
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a typo

| 'small'
| 'medium'
| 'large'
| 'xlarge'
| 'xxlarge'
| 'xxxlarge'
| 'xxxxlarge';

export type BoxProps = {
/**
* Shadow size of the box
* @default undefined
*/
shadow?: 'xsmall' | 'small' | 'medium' | 'large' | 'xlarge';
shadow?: 'xs' | 'sm' | 'md' | 'lg' | 'xl' | OldShadowSizes;
/**
* Border color of the box
* @default undefined
Expand All @@ -19,14 +31,15 @@ export type BoxProps = {
* @default undefined
*/
borderRadius?:
| 'small'
| 'medium'
| 'large'
| 'xlarge'
| 'xxlarge'
| 'xxxlarge'
| 'xxxxlarge'
| 'full';
| 'sm'
| 'md'
| 'lg'
| 'xl'
| '2xl'
| '3xl'
| '4xl'
| 'full'
| OldBorderRadii;
/**
* Background color of the box
* @default 'default'
Expand Down Expand Up @@ -54,14 +67,16 @@ export const Box = forwardRef<HTMLDivElement, BoxProps>(
ref,
) => {
const Component = asChild ? Slot : 'div';
const shadowSize = shadow && getSize(shadow);
const borderRadiusSize = borderRadius && getSize(borderRadius);

return (
<Component
ref={ref}
className={cl(
shadow && `fds-box--${shadow}-shadow`,
shadowSize && `fds-box--${shadowSize}-shadow`,
borderColor && `fds-box--${borderColor}-border-color`,
borderRadius && `fds-box--${borderRadius}-border-radius`,
borderRadiusSize && `fds-box--${borderRadiusSize}-border-radius`,
`fds-box--${background}-background`,
className,
)}
Expand Down
Loading