Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add a possibility to use button component as link #327

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/button/src/Button.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ export default {
options: getOptions(ButtonColor),
control: 'inline-radio',
},
href: {
options: ['', '#'],
control: {
type: 'inline-radio',
labels: { '#': 'Yes', '': 'No' },
},
},
},
} as Meta

Expand Down
14 changes: 10 additions & 4 deletions packages/button/src/Button.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ForwardedRef, forwardRef } from 'react'
import React, { ForwardedRef, forwardRef } from 'react'
import {
ButtonStyle,
ButtonContentStyle,
ButtonLoaderStyle,
} from './ButtonStyles'
import { ButtonProps } from './types'
import { ButtonButtonProps, ButtonProps } from './types'
import { useRipple } from './useRipple'

const loaderSizes = {
Expand All @@ -15,7 +15,10 @@ const loaderSizes = {
lg: 'medium',
} as const

function Button(props: ButtonProps, ref?: ForwardedRef<HTMLButtonElement>) {
function Button(
props: ButtonProps,
ref?: ForwardedRef<HTMLButtonElement | HTMLAnchorElement>
) {
const {
size = 'md',
variant = 'filled',
Expand All @@ -27,14 +30,16 @@ function Button(props: ButtonProps, ref?: ForwardedRef<HTMLButtonElement>) {
onClick,
disabled,
children,
href,
...rest
} = props
} = props as ButtonButtonProps
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a disabled appearance of a button even if it is a link

Copy link
Contributor

@Jabher Jabher Jul 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ButtonButton feels like this, maybe better naming can be here (e.g. NativeButton):


const { handleClick, ripple } = useRipple(props)
const loaderSize = loaderSizes[size]

return (
<ButtonStyle
as={href ? ('a' as React.ElementType) : undefined}
$size={size}
$variant={variant}
$fullwidth={fullwidth}
Expand All @@ -46,6 +51,7 @@ function Button(props: ButtonProps, ref?: ForwardedRef<HTMLButtonElement>) {
disabled={disabled || loading}
type='button'
ref={ref}
href={href}
Copy link
Contributor

@hexnickk4997 hexnickk4997 Jul 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not the best way from types standpoint, because <button> props and <a> props are different:

image

image

So basically we need that button should accept button props, and link should accept link props

cc @Jabher for discussion

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you suggest in this situation?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally like this approach, it is nice and minimal - by providing href you expect it to still be same component visually while making it a link. however, you are poiting different thing right - e.g. target attribute should be also supported here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, links and buttons have different props, and we probably should be able to specify which to use with link, otherwise there may be more prs and more if statements

{...rest}
>
<ButtonContentStyle $hidden={loading}>{children}</ButtonContentStyle>
Expand Down
2 changes: 2 additions & 0 deletions packages/button/src/ButtonStyles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ const variants = {
}

export const ButtonStyle = styled.button<InjectedProps>`
display: inline-block;
box-sizing: border-box;
margin: 0;
border: none;
Expand All @@ -179,6 +180,7 @@ export const ButtonStyle = styled.button<InjectedProps>`
background: transparent;
font-family: inherit;
font-weight: 700;
text-decoration: none;
width: ${({ $fullwidth }) => ($fullwidth ? ' 100%' : 'auto')};

:before {
Expand Down
3 changes: 3 additions & 0 deletions packages/button/src/__snapshots__/Button.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

exports[`renders correctly 1`] = `
.c0 {
display: inline-block;
box-sizing: border-box;
margin: 0;
border: none;
Expand All @@ -12,6 +13,8 @@ exports[`renders correctly 1`] = `
background: transparent;
font-family: inherit;
font-weight: 700;
-webkit-text-decoration: none;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we have any prefixer for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a test snapshot, tests see prefixed code:)

text-decoration: none;
width: auto;
line-height: 1em;
font-size: 14px;
Expand Down
20 changes: 19 additions & 1 deletion packages/button/src/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export enum ButtonColor {
}
export type ButtonColors = keyof typeof ButtonColor

export type ButtonProps = LidoComponentProps<
export type ButtonButtonProps = LidoComponentProps<
'button',
{
size?: ButtonSizes
Expand All @@ -39,9 +39,27 @@ export type ButtonProps = LidoComponentProps<
loading?: boolean
active?: boolean
as?: never
href?: never | undefined
}
>

type ButtonLinkProps = LidoComponentProps<
'a',
{
size?: ButtonSizes
variant?: ButtonVariants
color?: ButtonColors
fullwidth?: boolean
square?: boolean
loading?: boolean
active?: boolean
as?: never
href: string
}
>

export type ButtonProps = ButtonButtonProps | ButtonLinkProps

export type ButtonIconProps = {
icon: React.ReactNode
} & ButtonProps
5 changes: 4 additions & 1 deletion packages/button/src/useRipple.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ export const useRipple: UseRipple = ({ onClick }) => {
const [ripple, setRipple] = useState<React.ReactNode | null>(null)

const handleClick = useCallback(
(event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
(
event: React.MouseEvent<HTMLAnchorElement, MouseEvent> &
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it & here? It's probably anchor element | button element

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works only with &

React.MouseEvent<HTMLButtonElement, MouseEvent>
) => {
const button = event.currentTarget
const rect = button.getBoundingClientRect()
const diameter = Math.max(button.clientWidth, button.clientHeight)
Expand Down