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: add Spacer component #248

Merged
merged 2 commits into from
Jul 24, 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
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ export { FormControl } from "./form-control";
export { HelpBlock } from "./help-block";
export { Row } from "./row";
export { Modal, type ModalProps, type HeaderProps } from "./modal";
export { Spacer } from "./spacer";
7 changes: 3 additions & 4 deletions src/modal/modal.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { useState } from "react";
import { StoryObj, StoryFn, Meta } from "@storybook/react";

import { Button } from "../button";
import { Spacer } from "../spacer";
import { Modal } from "./modal";
import {
type ModalProps,
Expand Down Expand Up @@ -41,8 +42,6 @@ const story = {
},
} satisfies Meta<typeof Modal>;

const Spacer = () => <div style={{ height: "5px", width: "100%" }} />;

const DefaultTemplate: StoryFn<StoryProps> = ({
showCloseButton,
bodyAlignment,
Expand Down Expand Up @@ -84,7 +83,7 @@ const DefaultTemplate: StoryFn<StoryProps> = ({
<Button block size="large">
Submit
</Button>
<Spacer />
<Spacer size="xxs" />
<Button block size="large" onClick={handleClose}>
Cancel
</Button>
Expand Down Expand Up @@ -129,7 +128,7 @@ const DangerTemplate: StoryFn<ModalProps> = (args) => {
<Button block size="large" onClick={handleClose}>
Cancel
</Button>
<Spacer />
<Spacer size="xxs" />
<Button block variant="danger" size="large">
Submit
</Button>
Expand Down
1 change: 1 addition & 0 deletions src/spacer/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { Spacer } from "./spacer";
70 changes: 70 additions & 0 deletions src/spacer/spacer.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import React from "react";
import { Meta, StoryFn, StoryObj } from "@storybook/react";
import { Spacer } from "./spacer";
import { Button } from "../button";

const story = {
title: "Components/Spacer",
component: Spacer,
} satisfies Meta<typeof Spacer>;

type Story = StoryObj<typeof Spacer>;

const Template: StoryFn<typeof Spacer> = (args) => (
<>
<Button>Button one</Button>
<Spacer {...args} />
<Button>Button two</Button>
</>
);

export const XXS: Story = {
render: Template,
args: {
size: "xxs",
},
};

export const XS: Story = {
render: Template,
args: {
size: "xs",
},
};

export const S: Story = {
render: Template,
args: {
size: "s",
},
};

export const M: Story = {
render: Template,
args: {
size: "m",
},
};

export const L: Story = {
render: Template,
args: {
size: "l",
},
};

export const XL: Story = {
render: Template,
args: {
size: "xl",
},
};

export const XXL: Story = {
render: Template,
args: {
size: "xxl",
},
};

export default story;
31 changes: 31 additions & 0 deletions src/spacer/spacer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React from "react";

const sizes = {
xxs: "h-[5px]",
xs: "h-[10px]",
s: "h-[20px]",
m: "h-[30px]",
l: "h-[60px]",
xl: "h-[90px]",
xxl: "h-[180px]",
} as const;

interface SpacerProps {
Copy link
Member Author

Choose a reason for hiding this comment

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

The /learn Spacer accepts children, which was needed to account for a section on the profile page (freeCodeCamp/freeCodeCamp#49828). The rendering logic of the page has changed and I'm not seeing this kind of usage anywhere in /learn, so I'm not including this feature in the new version.

IMHO, I don't think Spacer should accept any children. As the name implies, it should just be a simple block or gap. Anything fancier should be handled by a layout component (#13).

/**
* Sizes:
* - xxs: 5px
* - xs: 10px
* - s: 20px
* - m: 30px
* - l: 60px
* - xl: 90px
* - xxl: 180px
*/
Comment on lines +14 to +23
Copy link
Member Author

Choose a reason for hiding this comment

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

I thought the annotation would save devs a trip to the component docs / implementation to look up the value.

Screenshot 2024-07-24 at 03 16 40

size: keyof typeof sizes;
}

export const Spacer = ({ size }: SpacerProps) => {
// NOTE: Do not construct class names dynamically
// https://tailwindcss.com/docs/content-configuration#classes-arent-generated
return <div className={sizes[size]} />;
};