Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Button padding change #272

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
34 changes: 28 additions & 6 deletions src/components/Button/Button.style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ export const ButtonStyled = styled.button<any>`

const sizePads = {
xxs: 0.125,
xs: 0.25,
sm: 0.5,
xs: 0.3125,
sm: 0.625,
md: 0.75,
lg: 1,
xl: 1.25,
Expand Down Expand Up @@ -134,24 +134,46 @@ function buttonMotion({ theme }) {

function buttonPadding({ icon, iconOnly, iconPosition, size }) {
return (
!iconOnly && iconButtonPadding(icon, iconPosition, sizePads[size])
!iconOnly &&
iconButtonPadding(icon, iconPosition, sizePads[size], size)
);
}

function iconButtonPadding(icon, iconPosition, pad) {
function iconButtonPadding(icon, iconPosition, pad, size) {
const minHeight = rem(3);
const minWidth = `${pad * 4 + 2}rem`;

// Some button sizes are "off" the padding formula based on design decions
const sizeAdjustments = {
xxs: { left: 0, right: 0 },
xs: { left: 0.065, right: 0.19125 },
sm: { left: -0.125, right: -0.125 },
md: { left: 0, right: 0 },
lg: { left: 0, right: 0 },
xl: { left: 0, right: 0 },
xxl: { left: 0, right: 0 },
};

switch (icon && iconPosition) {
case 'left':
return {
padding: '0 ' + pad + 'rem',
padding:
'0 ' +
(pad + sizeAdjustments[size].right) +
'rem 0 ' +
(pad + sizeAdjustments[size].left) +
'rem',
minHeight,
minWidth,
};
case 'right':
return {
padding: '0 ' + pad + 'rem',
padding:
'0 ' +
(pad + sizeAdjustments[size].right) +
'rem 0 ' +
(pad + sizeAdjustments[size].left) +
'rem',
minHeight,
minWidth,
};
Expand Down
54 changes: 54 additions & 0 deletions src/components/Button/Button.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,58 @@ describe('Button', () => {

expect(button).toHaveAttribute('disabled');
});

it('Change padding based on size', () => {
const { rerender } = renderWithThemeProvider(
<Button icon={<Pencil />} size="xs">
Hello
</Button>
);

const button = screen.getByRole('button');

expect(button).toHaveStyle({
padding: '0px 0.50375rem 0px 0.3775rem',
});

rerender(
<Button icon={<Pencil />} size="sm">
Hello
</Button>
);

expect(button).toHaveStyle({
padding: '0px 0.5rem 0px 0.5rem',
});

rerender(
<Button icon={<Pencil />} size="md">
Hello
</Button>
);

expect(button).toHaveStyle({
padding: '0px 0.75rem 0px 0.75rem',
});

rerender(
<Button icon={<Pencil />} size="lg">
Hello
</Button>
);

expect(button).toHaveStyle({
padding: '0px 1rem 0px 1rem',
});

rerender(
<Button icon={<Pencil />} size="xl">
Hello
</Button>
);

expect(button).toHaveStyle({
padding: '0px 1.25rem 0px 1.25rem',
});
});
});