|
| 1 | +import React from 'react'; |
| 2 | +import type { Meta, StoryObj } from '@storybook/react'; |
| 3 | +import { |
| 4 | + Title, |
| 5 | + Primary, |
| 6 | + Controls, |
| 7 | + Stories, |
| 8 | + Source as StorybookSource, |
| 9 | +} from '@storybook/addon-docs/blocks'; |
| 10 | + |
| 11 | +import './button.css'; |
| 12 | +import '../badge/badge.css'; |
| 13 | +import '../icon/icon.css'; |
| 14 | +import cssCode from './button.css?raw'; |
| 15 | +import { getIconHtml } from '../icon/icon.helper'; |
| 16 | + |
| 17 | +type ButtonArgs = { |
| 18 | + type?: 'primary' | 'secondary' | 'tertiary'; |
| 19 | + size?: 'small' | 'regular' | 'large'; |
| 20 | + text: string; |
| 21 | + url?: string; |
| 22 | + icon?: 'download' | 'right-arrow' | 'heart'; |
| 23 | + icon_only?: boolean; |
| 24 | + icon_placement?: 'before' | 'after'; |
| 25 | + is_disabled?: boolean; |
| 26 | + is_new_window?: boolean; |
| 27 | + badge_content?: string; |
| 28 | + modifier_class?: string; |
| 29 | +}; |
| 30 | + |
| 31 | +const generateHtmlCode = (args: ButtonArgs) => { |
| 32 | + const isAnchor = !!args.url; |
| 33 | + const tag = isAnchor ? 'a' : 'button'; |
| 34 | + const iconHtml = args.icon ? getIconHtml({ |
| 35 | + symbol: args.icon, |
| 36 | + size: args.size, |
| 37 | + modifier_class: 'button__icon' |
| 38 | + }) : ''; |
| 39 | + |
| 40 | + const classes = [ |
| 41 | + 'button', |
| 42 | + `button--${args.type || 'primary'}`, |
| 43 | + `button--${args.size || 'regular'}`, |
| 44 | + args.icon_only ? 'button--icon-only' : '', |
| 45 | + args.badge_content ? 'button--with-badge' : '', |
| 46 | + args.modifier_class || '', |
| 47 | + ].filter(Boolean).join(' '); |
| 48 | + |
| 49 | + const textHtml = `<span class="${args.icon_only ? 'visually-hidden' : 'button__text'}">${args.text}</span>`; |
| 50 | + const content = args.icon_placement === 'after' ? `${textHtml}${iconHtml}` : `${iconHtml}${textHtml}`; |
| 51 | + |
| 52 | + const attr = isAnchor |
| 53 | + ? `href="${args.url}" ${args.is_new_window ? 'target="_blank" rel="noopener"' : ''}` |
| 54 | + : `type="button" ${args.is_disabled ? 'disabled' : ''}`; |
| 55 | + |
| 56 | + return ` |
| 57 | +<${tag} class="${classes}" ${attr}> |
| 58 | + ${content} |
| 59 | + ${args.badge_content ? `<span class="badge badge--corner badge--red">${args.badge_content}</span>` : ''} |
| 60 | +</${tag}>`.trim(); |
| 61 | +}; |
| 62 | + |
| 63 | +const meta: Meta<ButtonArgs> = { |
| 64 | + title: 'Atoms/Button', |
| 65 | + render: (args) => ( |
| 66 | + <div dangerouslySetInnerHTML={{ __html: generateHtmlCode(args) }} style={{ display: 'contents' }} /> |
| 67 | + ), |
| 68 | + parameters: { |
| 69 | + layout: 'centered', |
| 70 | + docs: { |
| 71 | + page: () => ( |
| 72 | + <> |
| 73 | + <Title /> |
| 74 | + <Primary /> |
| 75 | + <Controls /> |
| 76 | + <Stories /> |
| 77 | + <div> |
| 78 | + <h3>Component CSS</h3> |
| 79 | + <StorybookSource |
| 80 | + code={cssCode} |
| 81 | + language="css" |
| 82 | + dark={true} |
| 83 | + /> |
| 84 | + </div> |
| 85 | + </> |
| 86 | + ), |
| 87 | + source: { |
| 88 | + transform: (_input, storyContext) => generateHtmlCode(storyContext.args as ButtonArgs), |
| 89 | + language: 'html', |
| 90 | + format: true, |
| 91 | + }, |
| 92 | + }, |
| 93 | + }, |
| 94 | + tags: ['autodocs'], |
| 95 | + argTypes: { |
| 96 | + type: { control: 'select', options: ['primary', 'secondary', 'tertiary'] }, |
| 97 | + size: { control: 'select', options: ['small', 'regular', 'large'] }, |
| 98 | + icon: { control: 'select', options: ['right-arrow', 'download', 'heart'] }, |
| 99 | + icon_placement: { control: 'inline-radio', options: ['before', 'after'] }, |
| 100 | + is_disabled: { control: 'boolean' }, |
| 101 | + icon_only: { control: 'boolean' }, |
| 102 | + badge_content: { control: 'text' }, |
| 103 | + }, |
| 104 | +}; |
| 105 | + |
| 106 | +export default meta; |
| 107 | + |
| 108 | +type Story = StoryObj<ButtonArgs>; |
| 109 | + |
| 110 | +export const PrimaryDefault: Story = { |
| 111 | + args: { |
| 112 | + type: 'primary', |
| 113 | + text: 'Order item', |
| 114 | + icon: 'right-arrow', |
| 115 | + icon_placement: 'before', |
| 116 | + }, |
| 117 | +}; |
| 118 | + |
| 119 | +export const WithIcon: Story = { |
| 120 | + args: { |
| 121 | + ...PrimaryDefault.args, |
| 122 | + icon: 'heart', |
| 123 | + text: 'Like', |
| 124 | + }, |
| 125 | +}; |
| 126 | + |
| 127 | +export const LinkButton: Story = { |
| 128 | + args: { |
| 129 | + type: 'secondary', |
| 130 | + text: 'Download PDF', |
| 131 | + url: 'https://example.com/spec.pdf', |
| 132 | + is_new_window: true, |
| 133 | + icon: 'download', |
| 134 | + }, |
| 135 | +}; |
0 commit comments