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

refactor: connect ShadCN Link to ShadCN ButtonLink #13560

Merged
Merged
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
2 changes: 1 addition & 1 deletion src/components/Footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import Translation from "@/components/Translation"
import { cn } from "@/lib/utils/cn"
import { scrollIntoView } from "@/lib/utils/scrollIntoView"

import { BaseLink } from "../../tailwind/Link"
import { Button } from "../../tailwind/ui/buttons/Button"
import { BaseLink } from "../../tailwind/ui/Link"

import { List, ListItem } from "./ui/list"

Expand Down
2 changes: 1 addition & 1 deletion src/components/SkipLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useTranslation } from "next-i18next"

import { MAIN_CONTENT_ID } from "@/lib/constants"

import { BaseLink } from "../../tailwind/Link"
import { BaseLink } from "../../tailwind/ui/Link"

export const SkipLink = () => {
const { t } = useTranslation()
Expand Down
Copy link
Member

Choose a reason for hiding this comment

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

👍🏼

File renamed without changes.
27 changes: 1 addition & 26 deletions tailwind/ui/__stories__/Button.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { MdChevronRight, MdExpandMore, MdNightlight } from "react-icons/md"
import { MdChevronRight, MdExpandMore } from "react-icons/md"
import type { Meta, StoryObj } from "@storybook/react"

import Translation from "@/components/Translation"

import { HStack, VStack } from "../../../src/components/ui/flex"
import { Button, type ButtonVariantProps } from "../buttons/Button"
import { ButtonLink } from "../buttons/Button"

const meta = {
title: "Atoms / Form / ShadCn Buttons",
Expand Down Expand Up @@ -105,25 +102,3 @@ export const MultiLineText: Story = {
</HStack>
),
}

export const OverrideStyles: Story = {
render: () => (
<>
<p>
Show custom styling examples here for visual testing of overrides from
the theme config
</p>
<VStack>
<Button aria-label="toggle" className="px-1.5">
<MdNightlight />
</Button>
<ButtonLink
className="rounded-full px-0 py-0"
linkProps={{ href: "#" }}
>
<Translation id="get-involved" />
</ButtonLink>
</VStack>
</>
),
}
17 changes: 17 additions & 0 deletions tailwind/ui/__stories__/ButtonLink.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { Meta, StoryObj } from "@storybook/react/*"

import { ButtonLink as ButtonLinkComponent } from "../buttons/Button"

const meta = {
title: "Atoms / Form / ShadCn Buttons",
component: ButtonLinkComponent,
} satisfies Meta<typeof ButtonLinkComponent>

export default meta

export const ButtonLink: StoryObj<typeof meta> = {
args: {
href: "#",
children: "What is Ethereum?",
},
}
23 changes: 8 additions & 15 deletions tailwind/ui/buttons/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ import * as React from "react"
import { cva, type VariantProps } from "class-variance-authority"
import { Slot } from "@radix-ui/react-slot"

import { BaseLink, type LinkProps } from "@/components/Link"

import { cn } from "@/lib/utils/cn"
import { type MatomoEventOptions, trackCustomEvent } from "@/lib/utils/matomo"
import { scrollIntoView } from "@/lib/utils/scrollIntoView"

import { BaseLink, type LinkProps } from "../Link"

const buttonVariants = cva(
"pointer inline-flex gap-2 items-center justify-center rounded border border-solid border-current text-primary transition focus-visible:outline focus-visible:outline-4 focus-visible:outline-primary-hover focus-visible:-outline-offset-1 disabled:text-disabled disabled:pointer-events-none hover:text-primary-hover [&[data-secondary='true']]:text-body [&>svg]:flex-shrink-0",
{
variants: {
variant: {
solid:
"!text-background bg-primary border-transparent disabled:bg-disabled disabled:text-background hover:text-background hover:bg-primary-hover hover:shadow-button-hover active:shadow-none",
"text-background bg-primary border-transparent disabled:bg-disabled disabled:text-background hover:text-background hover:bg-primary-hover hover:shadow-button-hover active:shadow-none",
outline: "hover:shadow-button-hover active:shadow-none",
ghost: "border-transparent",
link: "border-transparent font-bold underline py-0 px-1 active:text-primary",
Expand Down Expand Up @@ -106,29 +106,22 @@ const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
)
Button.displayName = "Button"

type ButtonLinkProps = ButtonProps & {
linkProps: LinkProps
type ButtonLinkProps = Omit<LinkProps, "onClick"> & {
buttonProps?: ButtonProps
customEventOptions?: MatomoEventOptions
}

const ButtonLink = React.forwardRef<HTMLAnchorElement, ButtonLinkProps>(
({ linkProps, customEventOptions, children, ...buttonProps }, ref) => {
({ buttonProps, customEventOptions, children, ...linkProps }, ref) => {
const handleClick = () => {
customEventOptions && trackCustomEvent(customEventOptions)
}
return (
<Button asChild {...buttonProps}>
<BaseLink
ref={ref}
activeStyle={{}}
// TODO: Redress this override when migrating the link component
color={
buttonProps.variant === "solid" ? "background.base" : undefined
}
textDecor="none"
_hover={{
textDecor: "none",
}}
className="no-underline hover:no-underline"
activeClassName=""
{...linkProps}
onClick={handleClick}
>
Expand Down
20 changes: 17 additions & 3 deletions tailwind/ui/buttons/ButtonTwoLines.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,20 @@ const ButtonTwoLines = ({
)

if (props.componentType === "link") {
const { buttonProps, ...rest } = props
return (
<ButtonLink className={commonClassStyles} size={size} {...props}>
<ChildContent {...props} size={size} isIconLeft={isIconLeft} />
<ButtonLink
className={commonClassStyles}
// size={size}
Copy link
Member

Choose a reason for hiding this comment

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

Did comment this out to skip a type error that was breaking the build. I see this as a minor issue for now since this component is not currently used. We can tackle the issue in a separate PR.

I also think that <ButtonTwoLines componentType="link" /> is not working. Something we can look into later.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

With componentType, the type guarding is not working as expected. Need to re-evaluate the approach.

With size, it is housed in buttonProps when passing props to ButtonLink. Not going to try and make the approach any more strict, but can simply run a merge props of buttonProps with size.

if (props.componentType === "link") {
  const { buttonProps, ...rest } = props

  const mergedButtonProps = merge(buttonProps, { size })
  return (
    <ButtonLink
      className={commonClassStyles}
      buttonProps={mergedButtonProps}
      {...rest}
    >
      <ChildContent
        {...rest}
        size={size}
        isSecondary={buttonProps?.isSecondary}
        isIconLeft={isIconLeft}
      />
    </ButtonLink>
  )
}

What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

I think we should follow the same approach we took with the original ButtonLink. In this case, I think we should return just a button to simplify everything. Then, in case you need a buttonlink, the user/dev could do the same we are doing with the buttonlink.

// always a button
<ButtonTwoLines size=".." variant="..." />

// in case you want a buttonlink => compose them with asChild (same as with ButtonLink)
<ButtonTwoLines asChild>
  <BaseLink> ...
</Button>

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@pettinarip Good with me! Getting too DRY here, and it's showing its cracks

buttonProps={buttonProps}
{...rest}
>
<ChildContent
{...rest}
size={size}
isSecondary={buttonProps?.isSecondary}
isIconLeft={isIconLeft}
/>
</ButtonLink>
)
}
Expand All @@ -79,7 +90,10 @@ const ButtonTwoLines = ({
export default ButtonTwoLines

const ChildContent = (
props: Omit<ButtonTwoLinesProps, "iconAlignment"> & { isIconLeft: boolean }
props: Omit<ButtonTwoLinesProps, "iconAlignment" | "buttonProps"> & {
isIconLeft: boolean
isSecondary?: boolean
}
) => {
const {
reverseTextOrder = false,
Expand Down
Loading