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 2 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
5 changes: 4 additions & 1 deletion packages/button/src/Button.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ForwardedRef, forwardRef } from 'react'
import React, { ForwardedRef, forwardRef } from 'react'
import {
ButtonStyle,
ButtonContentStyle,
Expand Down Expand Up @@ -27,6 +27,7 @@ function Button(props: ButtonProps, ref?: ForwardedRef<HTMLButtonElement>) {
onClick,
disabled,
children,
href,
...rest
} = props

Expand All @@ -35,6 +36,7 @@ function Button(props: ButtonProps, ref?: ForwardedRef<HTMLButtonElement>) {

return (
<ButtonStyle
as={href ? ('a' as React.ElementType) : undefined}
$size={size}
$variant={variant}
$fullwidth={fullwidth}
Expand All @@ -46,6 +48,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
1 change: 1 addition & 0 deletions packages/button/src/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export type ButtonProps = LidoComponentProps<
loading?: boolean
active?: boolean
as?: never
href?: string
}
>

Expand Down