Skip to content

Commit

Permalink
Merge pull request #122 from 8845musign/imple-pre
Browse files Browse the repository at this point in the history
Imple Pre Copmonent
  • Loading branch information
takanorip committed Jul 17, 2024
2 parents 4be9764 + e9f6b92 commit 4e19e3b
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 3 deletions.
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@
"fix:eslint": "npm run lint:eslint -- --fix",
"fix:prettier": "prettier -w src",
"fix:stylelint": "npm run lint:stylelint -- --fix",
"generate": "npm run generate:scaffold && npm run generate:barrel",
"generate:scaffold": "scaffdog generate component",
"generate:barrel": "node ./scripts/barrel.mjs",
"generate": "scaffdog generate component",
"prestorybook": "npm run build",
"storybook": "storybook dev -p 6006",
"build-storybook": "storybook build",
Expand Down
8 changes: 8 additions & 0 deletions src/components/Pre/Pre.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.pre {
display: block;
white-space: var(--white-space);
}

.pre.inline {
display: inline-block;
}
11 changes: 11 additions & 0 deletions src/components/Pre/Pre.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { render, screen } from '@testing-library/react';
import { Pre } from './Pre';

describe('<Pre>', () => {
it('receives data attributes', () => {
render(<Pre data-testid="pre" />);
const pre = screen.getByTestId('pre');

expect(pre).toBeInTheDocument();
});
});
76 changes: 76 additions & 0 deletions src/components/Pre/Pre.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { Meta, StoryObj } from '@storybook/react';
import { Pre } from './Pre';
import { Box } from '../Box/Box';

export default {
title: 'typography/Pre',
component: Pre,
} satisfies Meta<typeof Pre>;

type Story = StoryObj<typeof Pre>;

const defaultArgs = {
children: `Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.`,
};

export const Default: Story = {
render: (args) => (
<div style={{ width: 500 }}>
<Box border="gray" p="md">
<Pre {...args} />
</Box>
</div>
),
args: defaultArgs,
};

export const WhiteSpacePre: Story = {
render: (args) => (
<div style={{ width: 500 }}>
<Box border="gray" p="md">
<Pre {...args} />
</Box>
</div>
),
args: {
...defaultArgs,
whiteSpace: 'pre',
},
};

export const PreWrap: Story = {
render: (args) => (
<div style={{ width: 500 }}>
<Box border="gray" p="md">
<Pre {...args} />
</Box>
</div>
),
args: {
...defaultArgs,
whiteSpace: 'pre-wrap',
},
};

export const Inline: Story = {
render: (args) => (
<p>
Text
<Pre {...args} />
Text
</p>
),
args: {
...defaultArgs,
children: `Lorem Ipsum
is simply
dummy text
of the
printing and
typesetting industry.`,
inline: true,
},
};
41 changes: 41 additions & 0 deletions src/components/Pre/Pre.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use client';

import { clsx } from 'clsx';
import { CSSProperties, forwardRef, type HTMLAttributes, type PropsWithChildren } from 'react';
import styles from './Pre.module.css';

type AllowedSpanAttributes = Omit<HTMLAttributes<HTMLSpanElement>, 'className'>;

type Props = {
/**
* 折り返しや空白、改行の扱い
* @default 'pre-line'
*/
whiteSpace?: 'pre' | 'pre-wrap' | 'pre-line' | 'break-spaces';
/**
* inline-blockとして扱う
* @default false
*/
inline?: boolean;
} & AllowedSpanAttributes;

export const Pre = forwardRef<HTMLSpanElement, PropsWithChildren<Props>>(
({ children, whiteSpace = 'pre-line', inline = false, ...rest }, ref) => {
return (
<span
ref={ref}
className={clsx(styles.pre, inline && styles.inline)}
style={
{
'--white-space': whiteSpace,
} as CSSProperties
}
{...rest}
>
{children}
</span>
);
},
);

Pre.displayName = 'Pre';
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export { Label } from './components/Label/Label';
export { LinkCard } from './components/LinkCard/LinkCard';
export { MessageHalfModal } from './components/MessageHalfModal/MessageHalfModal';
export { MessageModal } from './components/MessageModal/MessageModal';
export { Pre } from './components/Pre/Pre';
export { RadioButton } from './components/RadioButton/RadioButton';
export { RadioCard } from './components/RadioCard/RadioCard';
export { RadioGroup } from './components/RadioGroup/RadioGroup';
Expand Down

0 comments on commit 4e19e3b

Please sign in to comment.