Skip to content

Commit

Permalink
feat: add docs for Button
Browse files Browse the repository at this point in the history
  • Loading branch information
Ricy committed Jan 6, 2024
1 parent 037fc01 commit e48138b
Show file tree
Hide file tree
Showing 4 changed files with 226 additions and 36 deletions.
6 changes: 3 additions & 3 deletions src/app/button/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Button from "@/components/Button";
import { StarIcon } from "@/components/Icons";
import Button from '@/components/Button';
import { StarIcon } from '@/components/Icons';

const ButtonPage: React.FC = () => {
return (
Expand All @@ -24,7 +24,7 @@ const ButtonPage: React.FC = () => {
<Button fullWidth>fullWidth</Button>
</div>
<div>
<Button color="white" loading="start">
<Button color="white" loading={true}>
loading
</Button>
</div>
Expand Down
26 changes: 16 additions & 10 deletions src/components/Button/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
}

button.fui-button {
@apply min-h-24px ;
@apply min-h-24px;
flex: 0 0 auto;
}

Expand All @@ -17,20 +17,21 @@ button.absolute {
}

/* size & shape */
/* props: width, height, radius, font size, line height*/
.fui-button--mini {
@apply text-[12px] h-[24px] px-[10px] rounded-4px leading-22px ;
@apply px-[10px] h-[24px] text-[12px] leading-22px rounded-4px;
}

.fui-button--small {
@apply text-[12px] h-[32px] px-[14px] rounded-4px leading-30px;
@apply px-[14px] h-[32px] text-[12px] leading-30px rounded-4px;
}

.fui-button--medium {
@apply text-[14px] h-[36px] px-[18px] rounded-6px leading-34px;
@apply px-[18px] h-[36px] text-[14px] leading-34px rounded-6px;
}

.fui-button--large {
@apply text-[14px] h-[40px] px-[22px] rounded-8px leading-38px;
@apply px-[22px] h-[40px] text-[14px] leading-38px rounded-8px;
}

/** disabled & loading **/
Expand All @@ -39,6 +40,7 @@ button.absolute {
@apply pointer-events-none;
}

/* different styles for loading&disable */
.is-loading {
@apply hover:opacity-90;
}
Expand All @@ -54,6 +56,7 @@ button.fui-button--text.is-disabled {
}

/* contained button */
/* bg, text color */
.fui-button--contained {
@apply border-none;
}
Expand All @@ -76,7 +79,9 @@ button.fui-button--text.is-disabled {
@apply bg-[#3AC170] text-#F1F1F3;
}

/* outlined dash button */
/* outlined button */
/* with border */
/* border width, border color, text color, bg */
.fui-button--outlined {
@apply border-1px;
}
Expand All @@ -99,13 +104,12 @@ button.fui-button--text.is-disabled {
@apply border-#EBEBED59 text-white bg-#FFFFFF14;
}


.fui-button--outlined.fui-button--green {
@apply border-#3AC170 text-#3AC170 bg-transparent;
}

.fui-button--outlined.fui-button--green:hover,
.fui-button--outlined.fui-button--green:focus-visible{
.fui-button--outlined.fui-button--green:focus-visible {
@apply border-#3AC170 text-#3AC170 bg-#3ac1702e;
}

Expand Down Expand Up @@ -166,7 +170,9 @@ button.fui-button--text.is-disabled {
content: '';
}

.fui-button.fui-button:active:not(.fui-button--text):not(.fui-button--link)::after {
.fui-button.fui-button:active:not(.fui-button--text):not(
.fui-button--link
)::after {
@apply opacity-60;
box-shadow: none;
transition: 0s;
Expand All @@ -177,5 +183,5 @@ button.fui-button--text.is-disabled {
}

.fui-button--contained.fui-button--green::after {
box-shadow: 0 0 0 6px #3AC170;
box-shadow: 0 0 0 6px #3ac170;
}
184 changes: 184 additions & 0 deletions src/components/Button/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
---
nav:
title: Button
path: /components
---

## Button

```tsx
/**
* title: variant & color
* desc: Button has 4 variants:contained(default)| outlined | text | link;3 colors:primary(default)| green |white。
* TODO: green and white don't support all variants now
*/
import React, { useState } from 'react';
import { Button } from 'custom-ui';

export default () => {
const [disabled, setDisabled] = useState(false);

return (
<>
<Button
variant="outlined"
size="small"
onClick={() => setDisabled((pre) => !pre)}
>
{disabled ? 'enable' : 'disable'}
</Button>

<div style={{ display: 'flex', gap: '24px', marginTop: '20px' }}>
<Button disabled={disabled}>Button</Button>
<Button variant="outlined" disabled={disabled}>
Button
</Button>
<Button variant="dash" disabled={disabled}>
Button
</Button>
<Button variant="text" disabled={disabled}>
Button
</Button>
<Button variant="link" disabled={disabled}>
Button
</Button>
</div>
<div style={{ display: 'flex', gap: '24px', marginTop: '20px' }}>
<Button color="secondary" disabled={disabled}>
Button
</Button>
<Button variant="outlined" color="secondary" disabled={disabled}>
Button
</Button>
<Button variant="dash" color="secondary" disabled={disabled}>
Button
</Button>
<Button variant="text" color="secondary" disabled={disabled}>
Button
</Button>
<Button variant="link" color="secondary" disabled={disabled}>
Button
</Button>
</div>
<div style={{ display: 'flex', gap: '24px', marginTop: '20px' }}>
<Button color="danger" disabled={disabled}>
Button
</Button>
<Button variant="outlined" color="danger" disabled={disabled}>
Button
</Button>
<Button variant="dash" color="danger" disabled={disabled}>
Button
</Button>
<Button variant="text" color="danger" disabled={disabled}>
Button
</Button>
<Button variant="link" color="danger" disabled={disabled}>
Button
</Button>
</div>
</>
);
};
```

```tsx
/**
* title: size & fullWidth
* desc: Button has 4 sizes:mini | small | medium(default) | large. When fullWidth is true ,it occupies the full width of its parent element。
*/
import React from 'react';
import { Button } from 'fluent-ui';

export default () => (
<>
<div style={{ display: 'flex', alignItems: 'center', gap: '24px' }}>
<Button size="mini">mini Button</Button>
<Button size="small">small Button</Button>
<Button>medium Button</Button>
<Button size="large">large Button</Button>
</div>

<div style={{ display: 'flex', gap: '24px', marginTop: '20px' }}>
<Button size="small" fullWidth>
fullWidth Button
</Button>
</div>
</>
);
```

```tsx
/**
* title: shape
* desc: Button has 3 shapes:rect(default) | circle | round。
* TODO: planned feature not supported yet
*/
import React from 'react';
import { Button, DownOutlined } from 'fluent-ui';

export default () => (
<>
<div style={{ display: 'flex', alignItems: 'center', gap: '24px' }}>
<Button>rect Button</Button>
<Button shape="round">round Button</Button>
<Button shape="circle">C</Button>
<Button shape="circle" color="secondary">
C
</Button>
<Button shape="circle" variant="dash" icon={DownOutlined}></Button>
<Button shape="circle" disabled>
C
</Button>
</div>
</>
);
```

```tsx
/**
* title: loading
* desc: when Button loading is true,the button is in loading status, unable to be clicked。
*/
import React from 'react';
import { Button, DownOutlined } from 'fluent-ui';

export default () => (
<>
<div style={{ display: 'flex', alignItems: 'center', gap: '24px' }}>
<Button loading>Button</Button>
<Button color="danger" loading>
Button
</Button>
<Button variant="outlined" loading>
Button
</Button>
<Button shape="circle" loading>
A
</Button>
</div>
</>
);
```

```tsx
/**
* title: href & <a>
* desc: when Button's href property is string,will be rendered as link <a/> 。usually used when variant is link ,for users that're used to opening links with right lick。
*/
import React from 'react';
import { Button, DownOutlined } from 'fluent-ui';

export default () => (
<>
<div style={{ display: 'flex', alignItems: 'center', gap: '24px' }}>
<Button href="https://www.baidu.com" target="_blank">
Button
</Button>
<Button variant="link" href="https://www.baidu.com" target="_blank">
Button
</Button>
</div>
</>
);
```
46 changes: 23 additions & 23 deletions src/components/Button/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,35 @@ import React, {
forwardRef,
type ReactNode,
type PropsWithChildren,
} from "react";
import cx from "clsx";
import Spin from "@/components/Spin";
import renderReactNode from "@/utils/renderReactNode";
import "./index.css";
} from 'react';
import cx from 'clsx';
import Spin from '@/components/Spin';
import renderReactNode from '@/utils/renderReactNode';
import './index.css';

export interface Props extends React.ButtonHTMLAttributes<HTMLButtonElement> {
variant?: "contained" | "outlined" | "dash" | "text" | "link";
color?: "primary" | "danger" | "green" | "white";
size?: "mini" | "small" | "medium" | "large";
shape?: "rect" | "circle" | "round";
variant?: 'contained' | 'outlined' | 'text' | 'link';
color?: 'primary' | 'green' | 'white';
size?: 'mini' | 'small' | 'medium' | 'large';
shape?: 'rect' | 'circle' | 'round';
fullWidth?: boolean;
loading?: boolean | "start" | "end";
loading?: boolean;
icon?: ReactNode;
startIcon?: ReactNode;
endIcon?: ReactNode;
href?: string;
target?: "_blank" | "_self" | "_parent" | "_top";
target?: '_blank' | '_self' | '_parent' | '_top';
}

const Button = forwardRef<HTMLButtonElement, PropsWithChildren<Props>>(
(
{
children,
className,
variant = "contained",
color = "primary",
size = "medium",
shape = "rect",
variant = 'contained',
color = 'primary',
size = 'medium',
shape = 'rect',
disabled = false,
fullWidth = false,
loading = false,
Expand All @@ -43,13 +43,13 @@ const Button = forwardRef<HTMLButtonElement, PropsWithChildren<Props>>(
_forwardRef
) => {
return createElement(
props.href ? "a" : "button",
props.href ? 'a' : 'button',
{
className: cx(
`fui-button fui-button--${variant} fui-button--${color} fui-button--${size} fui-button--${shape}`,
fullWidth && "fui-button--fullWidth",
loading === true && "is-loading",
disabled && "is-disabled",
fullWidth && 'fui-button--fullWidth',
loading && 'is-loading',
disabled && 'is-disabled',
className
),
ref: _forwardRef,
Expand All @@ -62,12 +62,12 @@ const Button = forwardRef<HTMLButtonElement, PropsWithChildren<Props>>(
{children && (
<div
className={cx(
"flex flex-row h-full",
loading && "cursor-not-allowed"
'flex flex-row h-full',
loading && 'cursor-not-allowed'
)}
>
<div className="w-24px h-24px">
{loading === "start" && <Spin className="mr-8px text-1.4em " />}
{loading && <Spin className="mr-8px text-1.4em " />}
</div>
{children}
</div>
Expand All @@ -78,7 +78,7 @@ const Button = forwardRef<HTMLButtonElement, PropsWithChildren<Props>>(
{endIcon && (
<span className="fui-button__icon">{renderReactNode(endIcon)}</span>
)}
{loading === true && <Spin className="fui-button__loading" />}
{loading && <Spin className="fui-button__loading" />}
</>
);
}
Expand Down

0 comments on commit e48138b

Please sign in to comment.