-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #94 from 8845musign/imple-icon-component
Imple `<Icon>` component
- Loading branch information
Showing
13 changed files
with
258 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
.icon { | ||
width: var(--size); | ||
height: var(--size); | ||
color: var(--text-color); | ||
vertical-align: bottom; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { composeStory } from '@storybook/react'; | ||
import { render, screen } from '@testing-library/react'; | ||
import Meta, { WithCustomDataAttribute, WithId } from './Icon.stories'; | ||
|
||
const WithCustomDataAttributeStory = composeStory(WithCustomDataAttribute, Meta); | ||
const WithIdStory = composeStory(WithId, Meta); | ||
|
||
describe('Icon', () => { | ||
test('Add custom data attributes', async () => { | ||
render(<WithCustomDataAttributeStory />); | ||
|
||
const iconElement = await screen.findByTestId('testid'); | ||
|
||
expect(iconElement).toBeInTheDocument(); | ||
}); | ||
|
||
test('Add id', async () => { | ||
render(<WithIdStory />); | ||
|
||
const iconElement = await screen.findByTestId('testid'); | ||
|
||
expect(iconElement).toHaveAttribute('id', 'icon-id'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { Meta, StoryObj } from '@storybook/react'; | ||
import { ComponentProps } from 'react'; | ||
import { Icon, Flex, Box, Stack } from '../../index'; | ||
|
||
export default { | ||
component: Icon, | ||
} satisfies Meta<typeof Icon>; | ||
|
||
const defaultArgs: Partial<ComponentProps<typeof Icon>> = { | ||
icon: 'UbieIcon', | ||
}; | ||
|
||
type Story = StoryObj<typeof Icon>; | ||
|
||
export const Default: Story = { | ||
render: (args) => <Icon {...args} />, | ||
args: defaultArgs, | ||
}; | ||
|
||
export const Size: Story = { | ||
render: (args) => ( | ||
<> | ||
<Flex alignItems="center" spacing="sm"> | ||
<Icon {...args} size="xs" /> | ||
<Icon {...args} size="sm" /> | ||
<Icon {...args} size="md" /> | ||
<Icon {...args} size="lg" /> | ||
<Icon {...args} size="xl" /> | ||
<Icon {...args} size="2xl" /> | ||
<Icon {...args} size="3xl" /> | ||
<Icon {...args} size="4xl" /> | ||
</Flex> | ||
</> | ||
), | ||
args: defaultArgs, | ||
}; | ||
|
||
export const Color: Story = { | ||
render: (args) => ( | ||
<Flex alignItems="center" spacing="sm"> | ||
<Icon {...args} color="primary" /> | ||
<Icon {...args} color="accent" /> | ||
<Icon {...args} color="main" /> | ||
<Icon {...args} color="sub" /> | ||
<Icon {...args} color="alert" /> | ||
<Icon {...args} color="link" /> | ||
<Icon {...args} color="linkSub" /> | ||
<Icon {...args} color="disabled" /> | ||
<Box pt="xxs" pr="xxs" pb="xxs" pl="xxs" backgroundColor="primaryDarken"> | ||
<Icon {...args} color="white" /> | ||
</Box> | ||
</Flex> | ||
), | ||
args: defaultArgs, | ||
}; | ||
|
||
export const CaseOnlyIcon: Story = { | ||
render: () => ( | ||
<Stack spacing="sm"> | ||
<p> | ||
ラベルや付随する文章を伴わずにアイコン単独で使う場合、アイコンが保つ意味を<code>label</code> propで付与します | ||
<br /> | ||
例: アイコンボタンなど | ||
</p> | ||
<div> | ||
<Icon icon="SetupIcon" label="設定" /> | ||
</div> | ||
</Stack> | ||
), | ||
}; | ||
|
||
export const WithCustomDataAttribute: Story = { | ||
render: (args) => <Icon {...args} />, | ||
args: { | ||
...defaultArgs, | ||
'data-testid': 'testid', | ||
}, | ||
}; | ||
|
||
export const WithId: Story = { | ||
render: (args) => <Icon {...args} />, | ||
args: { | ||
...defaultArgs, | ||
id: 'icon-id', | ||
'data-testid': 'testid', | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import * as Icons from '@ubie/ubie-icons'; | ||
import styles from './Icon.module.css'; | ||
import { CustomDataAttributeProps } from '../../types/attributes'; | ||
import { TextColor } from '../../types/style'; | ||
import { colorVariable } from '../../utils/style'; | ||
import type { FC, CSSProperties } from 'react'; | ||
|
||
type Icon = keyof typeof Icons; | ||
|
||
type IconSize = 'xs' | 'sm' | 'md' | 'lg' | 'xl' | '2xl' | '3xl' | '4xl'; | ||
|
||
const toIconSizeEmValue = (size: IconSize): string => { | ||
switch (size) { | ||
case 'xs': | ||
return '1em'; | ||
case 'sm': | ||
return '1.25em'; | ||
case 'md': | ||
return '1.5em'; | ||
case 'lg': | ||
return '1.75em'; | ||
case 'xl': | ||
return '2em'; | ||
case '2xl': | ||
return '4em'; | ||
case '3xl': | ||
return '5em'; | ||
case '4xl': | ||
return '6.5em'; | ||
default: | ||
// eslint-disable-next-line no-case-declarations | ||
const _: never = size; | ||
throw new Error(`Unknown size: ${_}`); | ||
} | ||
}; | ||
|
||
type Props = { | ||
/** | ||
* アイコンの種類 | ||
*/ | ||
icon: Icon; | ||
/** | ||
* 色。指定しない場合はinheritとなり、親要素のfont-colorを継承します | ||
*/ | ||
color?: TextColor; | ||
/** | ||
* サイズ | ||
* @default md | ||
*/ | ||
size?: IconSize; | ||
/** | ||
* ネイティブの`id`属性。ページで固有のIDを指定 | ||
*/ | ||
id?: string; | ||
/** | ||
* アイコンが何を表すかを説明するテキスト | ||
* 単に装飾的なアイコンの場合は指定しない | ||
*/ | ||
label?: string; | ||
} & CustomDataAttributeProps; | ||
|
||
/** | ||
* アイコンコンポーネント。labelを指定しない場合は単に装飾的なアイコンであるとみなされ、aria-hiddenが付与されます | ||
*/ | ||
export const Icon: FC<Props> = ({ icon, color, size = 'md', label, ...otherProps }) => { | ||
const IconComponent = Icons[icon]; | ||
const _sizeValue = toIconSizeEmValue(size); | ||
return ( | ||
<IconComponent | ||
role="img" | ||
aria-hidden={label === undefined || label === '' ? true : undefined} | ||
aria-label={label} | ||
className={styles.icon} | ||
style={ | ||
{ | ||
...colorVariable(color), | ||
'--size': _sizeValue, | ||
} as CSSProperties | ||
} | ||
{...otherProps} | ||
/> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters