Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

replace button

Jonathan Summers-Muir7fe480bb09ef
diff --git a/packages/ui/src/components/Button/Button.stories.tsx b/packages/ui/src/components/Button/Button.stories.tsx
index e73cac1cc86..3827669916e 100644
--- a/packages/ui/src/components/Button/Button.stories.tsx
+++ b/packages/ui/src/components/Button/Button.stories.tsx
@@ -1,235 +1,175 @@
-import { Button } from './'
-import { IconPackage } from './../Icon/icons/IconPackage'
-import { IconChevronRight } from './../Icon/icons/IconChevronRight'
+// Button.stories.ts|tsx
-export default {
+import type { Meta, StoryObj } from '@storybook/react'
+
+import { Button, ButtonProps } from './Button'
+
+import { IconAirplay } from '../Icon/icons/IconAirplay'
+
+const meta: Meta<typeof Button> = {
+ /* πŸ‘‡ The title prop is optional.
+ * See https://storybook.js.org/docs/react/configure/overview#configure-story-loading
+ * to learn how to generate automatic titles
+ */
title: 'General/Button',
component: Button,
}
-export const Neutral = (args: any) => <Button {...args}>Button text</Button>
-export const Success = (args: any) => <Button {...args}>Button text</Button>
-
-export const withStyles = (args: any) => <Button {...args}>Button text</Button>
-export const withIcon = (args: any) => <Button {...args}>Button text</Button>
-export const withIconRight = (args: any) => <Button {...args}>Button text</Button>
-export const withBlock = (args: any) => <Button {...args}>Button text</Button>
-export const withOnlyIcon = (args: any) => <Button {...args} />
-export const withOnlyLoading = (args: any) => <Button {...args} />
-export const withLoadingCentered = (args: any) => (
- <Button {...args}>Loading icon is centered</Button>
-)
-
-export const allButtons = (args: any) => (
- <>
- <div className="flex flex-col space-y-4">
- <div className="flex space-x-4">
- <Button {...args} size="tiny">
- Button text
- </Button>
- <Button {...args} size="tiny" type="default">
- Button text
- </Button>
- <Button {...args} size="tiny" type="secondary">
- Button text
- </Button>
- <Button {...args} size="tiny" type="alternative">
- Button text
- </Button>
- <Button {...args} size="tiny" type="link">
- Button text
- </Button>
- <Button {...args} size="tiny" type="text">
- Button text
- </Button>
- <Button {...args} size="tiny" type="dashed">
- Button text
- </Button>
- <Button {...args} size="tiny" type="outline">
- Button text
- </Button>
- <Button {...args} size="tiny" type="danger">
- Button text
- </Button>
- <Button {...args} size="tiny" type="warning">
- Button text
- </Button>
- </div>
-
- <div className="flex space-x-4">
- <Button {...args} size="small">
- Button text
- </Button>
- <Button {...args} size="small" type="default">
- Button text
- </Button>
- <Button {...args} size="small" type="secondary">
- Button text
- </Button>
- <Button {...args} size="small" type="alternative">
- Button text
- </Button>
- <Button {...args} size="small" type="link">
- Button text
- </Button>
- <Button {...args} size="small" type="text">
- Button text
- </Button>
- <Button {...args} size="small" type="dashed">
- Button text
- </Button>
- <Button {...args} size="small" type="outline">
- Button text
- </Button>
- <Button {...args} size="small" type="danger">
- Button text
- </Button>
- <Button {...args} size="small" type="warning">
- Button text
- </Button>
- </div>
- <div className="flex space-x-4">
- <Button {...args} size="medium">
- Button text
- </Button>
- <Button {...args} size="medium" type="default">
- Button text
- </Button>
- <Button {...args} size="medium" type="secondary">
- Button text
- </Button>
- <Button {...args} size="medium" type="alternative">
- Button text
- </Button>
- <Button {...args} size="medium" type="link">
- Button text
- </Button>
- <Button {...args} size="medium" type="text">
- Button text
- </Button>
- <Button {...args} size="medium" type="dashed">
- Button text
- </Button>
- <Button {...args} size="medium" type="outline">
- Button text
- </Button>
- <Button {...args} size="medium" type="danger">
- Button text
- </Button>
- <Button {...args} size="medium" type="warning">
- Button text
- </Button>
- </div>
- <div className="flex space-x-4">
- <Button {...args} size="large">
- Button text
- </Button>
- <Button {...args} size="large" type="default">
- Button text
- </Button>
- <Button {...args} size="large" type="secondary">
- Button text
- </Button>
- <Button {...args} size="large" type="alternative">
- Button text
- </Button>
- <Button {...args} size="large" type="link">
- Button text
- </Button>
- <Button {...args} size="large" type="text">
- Button text
- </Button>
- <Button {...args} size="large" type="dashed">
- Button text
- </Button>
- <Button {...args} size="large" type="outline">
- Button text
- </Button>
- <Button {...args} size="large" type="danger">
- Button text
- </Button>
- <Button {...args} size="large" type="warning">
- Button text
- </Button>
- </div>
- <div className="flex space-x-4">
- <Button {...args} size="xlarge">
- Button text
- </Button>
- <Button {...args} size="xlarge" type="default">
- Button text
- </Button>
- <Button {...args} size="xlarge" type="secondary">
- Button text
- </Button>
- <Button {...args} size="xlarge" type="alternative">
- Button text
- </Button>
- <Button {...args} size="xlarge" type="link">
- Button text
- </Button>
- <Button {...args} size="xlarge" type="text">
- Button text
- </Button>
- <Button {...args} size="xlarge" type="dashed">
- Button text
- </Button>
- <Button {...args} size="xlarge" type="outline">
- Button text
- </Button>
- <Button {...args} size="xlarge" type="danger">
- Button text
- </Button>
- <Button {...args} size="xlarge" type="warning">
- Button text
- </Button>
- </div>
- </div>
- </>
-)
-
-export const withCustomTag = (args: any) => <Button {...args}>Button text</Button>
-
-const icon = <IconPackage />
-
-withIcon.args = {
- type: 'primary',
- icon: icon,
-}
-
-withIconRight.args = {
- type: 'primary',
- iconRight: <IconChevronRight strokeWidth={2} />,
-}
-
-withStyles.args = {
- type: 'primary',
- style: { backgroundColor: 'red', color: 'yellow' },
-}
-
-withBlock.args = {
- type: 'primary',
- block: true,
-}
-
-withOnlyIcon.args = {
- icon: icon,
-}
-
-withOnlyLoading.args = {
- loading: true,
-}
-
-withLoadingCentered.args = {
- loading: true,
- loadingCentered: true,
-}
-
-allButtons.args = {
- loading: false,
- danger: false,
-}
-
-withCustomTag.args = {
- as: 'span',
+export default meta
+
+type Story = StoryObj<typeof Button>
+
+/*
+ *πŸ‘‡ Render functions are a framework specific feature to allow you control on how the component renders.
+ * See https://storybook.js.org/docs/react/api/csf
+ * to learn how to use render functions.
+ */
+
+export const Primary = {
+ args: {
+ children: 'Hello world',
+ type: 'primary',
+ },
+ render: ({ children, type }: ButtonProps) => <Button type={type}>{children}</Button>,
+}
+export const Default = {
+ args: {
+ children: 'Hello world',
+ type: 'default',
+ },
+ render: ({ children, type }: ButtonProps) => <Button type={type}>{children}</Button>,
+}
+
+export const Alternative = {
+ args: {
+ children: 'Hello world',
+ type: 'alternative',
+ },
+ render: ({ children, type }: ButtonProps) => <Button type={type}>{children}</Button>,
+}
+
+export const Outline = {
+ args: {
+ children: 'Hello world',
+ type: 'outline',
+ },
+ render: ({ children, type }: ButtonProps) => <Button type={type}>{children}</Button>,
+}
+
+export const Secondary = {
+ args: {
+ children: 'Hello world',
+ type: 'secondary',
+ },
+ render: ({ children, type }: ButtonProps) => <Button type={type}>{children}</Button>,
+}
+
+export const Dashed = {
+ args: {
+ children: 'Hello world',
+ type: 'dashed',
+ },
+ render: ({ children, type }: ButtonProps) => <Button type={type}>{children}</Button>,
+}
+
+export const Link = {
+ args: {
+ children: 'Hello world',
+ type: 'link',
+ },
+ render: ({ children, type }: ButtonProps) => <Button type={type}>{children}</Button>,
+}
+
+export const Text = {
+ args: {
+ children: 'Hello world',
+ type: 'text',
+ },
+ render: ({ children, type }: ButtonProps) => <Button type={type}>{children}</Button>,
+}
+
+export const Danger = {
+ args: {
+ children: 'Hello world',
+ type: 'danger',
+ },
+ render: ({ children, type }: ButtonProps) => <Button type={type}>{children}</Button>,
+}
+
+export const Block = {
+ args: {
+ children: 'Hello world',
+ type: 'primary',
+ block: true,
+ },
+ render: ({ children, type, block }: ButtonProps) => (
+ <Button block type={type}>
+ {children}
+ </Button>
+ ),
+}
+
+export const Disabled = {
+ args: {
+ children: 'Hello world',
+ type: 'primary',
+ disabled: true,
+ },
+ render: ({ children, type, disabled }: ButtonProps) => (
+ <Button disabled type={type}>
+ {children}
+ </Button>
+ ),
+}
+
+export const Sizes = {
+ args: {
+ children: 'Change the size below',
+ type: 'primary',
+ size: 'large',
+ },
+ render: ({ children, type, size }: ButtonProps) => (
+ <Button size={size} type={type}>
+ {children}
+ </Button>
+ ),
+}
+
+export const LeftIcon = {
+ args: {
+ children: 'Change the size below',
+ type: 'primary',
+ iconLeft: <IconAirplay />,
+ },
+ render: ({ children, type, iconLeft }: ButtonProps) => (
+ <Button iconLeft={iconLeft} type={type}>
+ {children}
+ </Button>
+ ),
+}
+
+export const RightIcon = {
+ args: {
+ children: 'Change the size below',
+ type: 'primary',
+ iconRight: <IconAirplay />,
+ },
+ render: ({ children, type, iconRight }: ButtonProps) => (
+ <Button iconRight={iconRight} type={type}>
+ {children}
+ </Button>
+ ),
+}
+
+export const Loading = {
+ args: {
+ children: 'Change the size below',
+ type: 'primary',
+ loading: true,
+ },
+ render: ({ children, type, loading }: ButtonProps) => (
+ <Button loading={loading} type={type}>
+ {children}
+ </Button>
+ ),
}
diff --git a/packages/ui/src/components/Button/Button.tsx b/packages/ui/src/components/Button/Button.tsx
index 145a9333ebb..944e9d43830 100644
--- a/packages/ui/src/components/Button/Button.tsx
+++ b/packages/ui/src/components/Button/Button.tsx
@@ -1,159 +1,192 @@
-import React, { forwardRef } from 'react'
-import { IconContext } from './../Icon/IconContext'
-import { IconLoader } from './../Icon/icons/IconLoader'
-
-import styleHandler from '../../lib/theme/styleHandler'
+import * as React from 'react'
+import { Slot } from '@radix-ui/react-slot'
+import { VariantProps, cva } from 'class-variance-authority'
+
+import { IconContext } from '../../Icon/IconContext'
+import { IconLoader } from '../../Icon/icons/IconLoader'
+
+import { cn } from '../../lib/utils'
+import { sizes } from '../../lib/commonCva'
+
+export type ButtonVariantProps = VariantProps<typeof buttonVariants>
+const buttonVariants = cva(
+ `relative
+ cursor-pointer
+ inline-flex
+ items-center
+ space-x-2
+ text-center
+ font-regular
+ ease-out
+ duration-200
+ rounded
+ outline-none
+ transition-all
+ outline-0
+ focus-visible:outline-4
+ focus-visible:outline-offset-1`,
+ {
+ variants: {
+ type: {
+ primary: `
+ bg-brand-fixed-1100 hover:bg-brand-fixed-1000
+ text-white
+ bordershadow-brand-fixed-1000 hover:bordershadow-brand-fixed-900 dark:bordershadow-brand-fixed-1000 dark:hover:bordershadow-brand-fixed-1000
+ focus-visible:outline-brand-600
+ shadow-sm`,
+ secondary: `
+ bg-scale-1200
+ text-scale-100 hover:text-scale-800
+ focus-visible:text-scale-600
+ bordershadow-scale-1100 hover:bordershadow-scale-900
+ focus-visible:outline-scale-700
+ shadow-sm`,
+ default: `
+ text-scale-1200
+ bg-scale-100 hover:bg-scale-300
+ bordershadow-scale-600 hover:bordershadow-scale-700
+ dark:bordershadow-scale-700 hover:dark:bordershadow-scale-800
+ dark:bg-scale-500 dark:hover:bg-scale-600
+ focus-visible:outline-brand-600
+ shadow-sm`,
+ alternative: `
+ text-brand-1100
+ bg-brand-200 hover:bg-brand-400
+ bordershadow-brand-600 hover:bordershadow-brand-800
+ dark:bordershadow-brand-700 hover:dark:bordershadow-brand-800
+ focus-visible:border-brand-800
+ focus-visible:outline-brand-600
+ shadow-sm`,
+ outline: `
+ text-scale-1200
+ bg-transparent
+ bordershadow-scale-600 hover:bordershadow-scale-700
+ dark:bordershadow-scale-800 hover:dark:bordershadow-scale-900
+ focus-visible:outline-scale-700`,
+ dashed: `
+ text-scale-1200
+ border
+ border-dashed
+ border-scale-700 hover:border-scale-900
+ bg-transparent
+ focus-visible:outline-scale-700
+ shadow-sm`,
+ link: `
+ text-brand-1100
+ border
+ border-transparent
+ hover:bg-brand-400
+ border-opacity-0
+ bg-opacity-0 dark:bg-opacity-0
+ shadow-none
+ focus-visible:outline-scale-700`,
+ text: `
+ text-scale-1200
+ hover:bg-scale-500
+ shadow-none
+ focus-visible:outline-scale-700`,
+ danger: `
+ text-red-1100
+ bg-red-200
+ bordershadow-red-700 hover:bordershadow-red-900
+ hover:bg-red-900
+ hover:text-lo-contrast
+ focus-visible:outline-red-700
+ shadow-sm`,
+ warning: `
+ text-amber-1100
+ bg-amber-200
+ bordershadow-amber-700 hover:bordershadow-amber-900
+ hover:bg-amber-900
+ hover:text-hi-contrast
+ focus-visible:outline-amber-700
+ shadow-sm`,
+ },
+ block: {
+ true: 'w-full flex items-center justify-center',
+ },
+ size: {
+ ...sizes,
+ },
+ overlay: {
+ base: `absolute inset-0 bg-scale-200 opacity-50`,
+ container: `fixed inset-0 transition-opacity`,
+ },
+ disabled: {
+ true: 'opacity-50 cursor-not-allowed pointer-events-none',
+ },
+ defaultVariants: {
+ // variant: 'default',
+ // size: 'default',
+ },
+ },
+ }
+)
-export interface ButtonProps extends React.HTMLAttributes<HTMLButtonElement> {
- block?: boolean
- className?: string
- children?: React.ReactNode
- disabled?: boolean
- onClick?: React.MouseEventHandler<HTMLButtonElement>
+export type LoadingVariantProps = VariantProps<typeof loadingVariants>
+const loadingVariants = cva('', {
+ variants: {
+ loading: {
+ default: '',
+ true: `animate-spin`,
+ },
+ },
+})
+
+export interface ButtonProps
+ // omit `type` as we use it to change type of button
+ // replaced with `htmlType`
+ extends Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, 'type'>,
+ // omit 'disabled' as it is included in HTMLButtonElement
+ Omit<ButtonVariantProps, 'disabled'>,
+ LoadingVariantProps {
+ asChild?: boolean
+ type?: ButtonVariantProps['type']
+ htmlType?: React.ButtonHTMLAttributes<HTMLButtonElement>['type']
icon?: React.ReactNode
+ iconLeft?: React.ReactNode
iconRight?: React.ReactNode
- loading?: boolean
- loadingCentered?: boolean
- shadow?: boolean
- size?: ButtonSize
- style?: React.CSSProperties
- type?: ButtonType
- danger?: boolean
- htmlType?: 'button' | 'submit' | 'reset'
- ref?: any
- ariaSelected?: boolean
- ariaControls?: string
- tabIndex?: 0 | -1
- role?: string
- textAlign?: 'left' | 'center' | 'right'
- as?: keyof JSX.IntrinsicElements
- form?: string
}
-export type ButtonSize = 'tiny' | 'small' | 'medium' | 'large' | 'xlarge'
-export type ButtonType =
- | 'primary'
- | 'default'
- | 'secondary'
- | 'alternative'
- | 'outline'
- | 'dashed'
- | 'link'
- | 'text'
- | 'danger'
- | 'warning'
-
-interface CustomButtonProps extends React.HTMLAttributes<HTMLButtonElement> {}
+const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
+ ({ asChild = false, size = 'small', type = 'primary', ...props }, ref) => {
+ const Comp = asChild ? Slot : 'button'
-export const Button = forwardRef<HTMLButtonElement, ButtonProps>(
- (
- {
- block,
- className,
- children,
- danger,
- disabled = false,
- onClick,
- icon,
- iconRight,
- loading = false,
- loadingCentered = false,
- shadow = true,
- size = 'tiny',
- style,
- type = 'primary',
- htmlType = 'button',
- ariaSelected,
- ariaControls,
- tabIndex,
- role,
- as,
- textAlign = 'center',
- ...props
- }: ButtonProps,
- ref
- ) => {
- let __styles = styleHandler('button')
+ const { className, disabled, loading, icon, iconLeft, iconRight } = props
- // styles
const showIcon = loading || icon
- let classes = [__styles.base]
- let containerClasses = [__styles.container]
-
- classes.push(__styles.type[type])
-
- if (block) {
- containerClasses.push(__styles.block)
- classes.push(__styles.block)
- }
-
- if (shadow && type !== 'link' && type !== 'text') {
- classes.push(__styles.shadow)
- }
-
- if (size) {
- classes.push(__styles.size[size])
- }
-
- if (className) {
- classes.push(className)
- }
-
- if (disabled) {
- classes.push(__styles.disabled)
- }
-
- const iconLoaderClasses = [__styles.loading]
-
- // custom button tag
- const CustomButton = ({ ...props }) => {
- const Tag = as as keyof JSX.IntrinsicElements
- return <Tag {...props} />
- }
+ // decrecating 'showIcon' for rightIcon
+ const _iconLeft: React.ReactNode = icon ?? iconLeft
const buttonContent = (
<>
{showIcon &&
(loading ? (
- <IconLoader size={size} className={iconLoaderClasses.join(' ')} />
- ) : icon ? (
- <IconContext.Provider value={{ contextSize: size }}>{icon}</IconContext.Provider>
+ <IconLoader size={size} className={cn(loadingVariants({ loading }))} />
+ ) : _iconLeft ? (
+ <IconContext.Provider value={{ contextSize: size }}>{_iconLeft}</IconContext.Provider>
) : null)}
- {children && <span className={__styles.label}>{children}</span>}
+ {props.children && <span className={'truncate'}>{props.children}</span>}
{iconRight && !loading && (
<IconContext.Provider value={{ contextSize: size }}>{iconRight}</IconContext.Provider>
)}
</>
)
- if (as) {
- return (
- <CustomButton {...props} className={classes.join(' ')} onClick={onClick} style={style}>
- {buttonContent}
- </CustomButton>
- )
- } else {
- return (
- // <span ref={containerRef} className={containerClasses.join(' ')}>
- <button
- {...props}
- ref={ref}
- className={classes.join(' ')}
- disabled={loading || (disabled && true)}
- onClick={onClick}
- style={style}
- type={htmlType}
- aria-selected={ariaSelected}
- aria-controls={ariaControls}
- tabIndex={tabIndex}
- role={role}
- form={props.form}
- >
- {buttonContent}
- </button>
- // </span>
- )
- }
+ return (
+ <Comp
+ className={cn(buttonVariants({ type, size, disabled }), className)}
+ ref={ref}
+ type={props.htmlType}
+ {...props}
+ >
+ {buttonContent}
+ </Comp>
+ )
}
)
+
+Button.displayName = 'Button'
+
+export { Button, buttonVariants }