Skip to content

Commit 89d9efa

Browse files
authored
Merge pull request #10823 from ethereum/fix-nav
Fix Nav styles to match DS header
2 parents b7b3841 + a409724 commit 89d9efa

File tree

11 files changed

+331
-314
lines changed

11 files changed

+331
-314
lines changed

src/@chakra-ui/gatsby-plugin/components/Button.ts

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,15 @@ import { defineStyle, defineStyleConfig } from "@chakra-ui/react"
1010
*/
1111
const ICON_SELECTOR = "& svg"
1212

13-
const getBaseColor = (isSecondary: boolean) =>
14-
!isSecondary ? "primary.base" : "body.base"
15-
16-
const baseStyle = defineStyle((props) => ({
13+
const baseStyle = defineStyle({
1714
borderRadius: "base",
1815
border: "1px",
19-
color: getBaseColor(props.isSecondary),
16+
color: "primary.base",
2017
lineHeight: "1.6",
2118
transitionProperty: "common",
2219
transitionDuration: "normal",
2320
whiteSpace: "normal",
21+
p: "unset",
2422
_focusVisible: {
2523
outline: "4px solid",
2624
outlineColor: "primary.hover",
@@ -33,7 +31,16 @@ const baseStyle = defineStyle((props) => ({
3331
_hover: {
3432
color: "primary.hover",
3533
},
36-
}))
34+
"&[data-secondary='true']": {
35+
color: "body.base",
36+
},
37+
"&.chakra-link": {
38+
textDecoration: "none",
39+
_hover: {
40+
textDecoration: "none",
41+
},
42+
},
43+
})
3744

3845
const variantSolid = defineStyle({
3946
color: "background.base",
@@ -68,7 +75,6 @@ const variantGhost = {
6875

6976
const variantLink = defineStyle({
7077
borderColor: "transparent",
71-
color: "primary.base",
7278
fontWeight: 700,
7379
textDecor: "underline",
7480
py: 0,
@@ -78,47 +84,29 @@ const variantLink = defineStyle({
7884
},
7985
})
8086

81-
/**
82-
* @deprecated This is no longer needed. Styling for just the icon is not
83-
* unique compared to the variants used for text (as of the new DS)
84-
*/
85-
const variantIcon = defineStyle({
86-
appearance: "none",
87-
background: "inherit",
88-
padding: "initial",
89-
border: 0,
90-
color: "inherit",
91-
boxShadow: "none",
92-
_hover: {
93-
color: "primary.base",
94-
boxShadow: "none",
95-
},
96-
})
97-
9887
const sizes = {
99-
md: {
100-
py: "2 !important",
101-
px: "4 !important",
88+
md: defineStyle({
89+
py: "2",
90+
px: "4",
10291
[ICON_SELECTOR]: {
10392
fontSize: "2xl",
10493
},
105-
},
106-
sm: {
94+
}),
95+
sm: defineStyle({
10796
fontSize: "xs",
108-
py: "1.5 !important",
109-
px: "2 !important",
97+
py: "1.5",
98+
px: "2",
11099
[ICON_SELECTOR]: {
111100
fontSize: "md",
112101
},
113-
},
102+
}),
114103
}
115104

116105
const variants = {
117106
solid: variantSolid,
118107
outline: variantOutline,
119108
ghost: variantGhost,
120109
link: variantLink,
121-
icon: variantIcon,
122110
}
123111

124112
export const Button = defineStyleConfig({

src/components/Button/Button.stories.tsx

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import * as React from "react"
2-
import { HStack, IconButton, ThemingProps, VStack } from "@chakra-ui/react"
2+
import { HStack, ThemingProps, Text, VStack } from "@chakra-ui/react"
33
import { getThemingArgTypes } from "@chakra-ui/storybook-addon"
44
import { Meta, StoryObj } from "@storybook/react"
5-
import { MdExpandMore, MdChevronRight } from "react-icons/md"
6-
import Button from "."
5+
import { MdExpandMore, MdChevronRight, MdNightlight } from "react-icons/md"
76
import theme from "../../@chakra-ui/gatsby-plugin/theme"
7+
import ButtonLink from "../ButtonLink"
8+
import IconButton from "../IconButton"
9+
import Translation from "../Translation"
10+
import Button from "."
811

912
type ButtonType = typeof Button
1013

@@ -45,15 +48,12 @@ export const StyleVariants: Story = {
4548
},
4649
render: (args) => (
4750
<VStack spacing={4}>
48-
{variants.map((variant, idx) => {
49-
if (args.isSecondary && variant === "solid") return
50-
return (
51-
<HStack spacing={4} key={idx}>
52-
<Button variant={variant} {...args} />
53-
<Button variant={variant} isDisabled {...args} />
54-
</HStack>
55-
)
56-
})}
51+
{variants.map((variant, idx) => (
52+
<HStack spacing={4} key={idx}>
53+
<Button variant={variant} {...args} />
54+
<Button variant={variant} isDisabled {...args} />
55+
</HStack>
56+
))}
5757
</VStack>
5858
),
5959
}
@@ -118,3 +118,20 @@ export const MultiLineText: Story = {
118118
</HStack>
119119
),
120120
}
121+
122+
export const OverrideStyles: Story = {
123+
render: () => (
124+
<>
125+
<Text>
126+
Show custom styling examples here for visual testing of overrides from
127+
the theme config
128+
</Text>
129+
<VStack>
130+
<IconButton aria-label="toggle" icon={<MdNightlight />} px="1.5" />
131+
<ButtonLink to="#" borderRadius="full" px="0" py="0">
132+
<Translation id="get-involved" />
133+
</ButtonLink>
134+
</VStack>
135+
</>
136+
),
137+
}

src/components/Button/index.tsx

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,42 @@ import React from "react"
22
import {
33
Button as ChakraButton,
44
ButtonProps,
5-
useStyleConfig,
5+
forwardRef,
66
} from "@chakra-ui/react"
77

88
import { scrollIntoView } from "../../utils/scrollIntoView"
99

10+
export const checkIsSecondary = (props: {
11+
variant?: string
12+
isSecondary?: boolean
13+
}) => {
14+
const { variant, isSecondary } = props
15+
// These two variants do not have secondary styling, so prevent overrides
16+
return {
17+
"data-secondary":
18+
!["solid", "link"].includes(variant || "solid") && isSecondary,
19+
}
20+
}
21+
1022
export interface IProps extends ButtonProps {
23+
/**
24+
* Set string value that matches the `id` attribute value used
25+
* on another element in a given page. Selecting the button will then
26+
* trigger a scroll to that element.
27+
*/
1128
toId?: string
29+
/**
30+
* Custom theme prop. If true, `body` color is used instead of
31+
* `primary` color in the theming.
32+
*
33+
* `NOTE`: Does not apply to the `Solid` or `Link` variants
34+
*/
1235
isSecondary?: boolean
1336
}
1437

15-
const Button: React.FC<IProps> = ({ toId, isSecondary, onClick, ...props }) => {
38+
const Button = forwardRef<IProps, "button">((props, ref) => {
39+
const { toId, onClick, isSecondary, ...rest } = props
40+
1641
const handleOnClick = (e: React.MouseEvent<HTMLButtonElement>) => {
1742
if (toId) {
1843
scrollIntoView(toId)
@@ -21,14 +46,14 @@ const Button: React.FC<IProps> = ({ toId, isSecondary, onClick, ...props }) => {
2146
onClick?.(e)
2247
}
2348

24-
/**
25-
* Prevent React warning that does not recognize `isSecondary` on DOM
26-
* while still sending prop to the theme config
27-
*/
28-
const styles = useStyleConfig("Button", { ...props, isSecondary })
29-
30-
// `styles` object sent to `sx` prop per convention
31-
return <ChakraButton onClick={handleOnClick} sx={styles} {...props} />
32-
}
49+
return (
50+
<ChakraButton
51+
ref={ref}
52+
onClick={handleOnClick}
53+
{...checkIsSecondary({ variant: rest.variant?.toString(), isSecondary })}
54+
{...rest}
55+
/>
56+
)
57+
})
3358

3459
export default Button

src/components/ButtonLink.tsx

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,13 @@
11
import React from "react"
2-
import { Button, ButtonProps, useStyleConfig } from "@chakra-ui/react"
32

3+
import type { IProps as IButtonProps } from "./Button"
44
import { BaseLink, IBaseProps as ILinkProps } from "./Link"
5+
import Button from "./Button"
56

6-
export interface IProps extends ILinkProps, ButtonProps {
7-
isSecondary?: boolean
8-
}
9-
10-
const ButtonLink: React.FC<IProps> = ({ children, isSecondary, ...props }) => {
11-
/**
12-
* Prevent React warning that does not recognize `isSecondary` on DOM
13-
* while still sending prop to the theme config
14-
*/
15-
const styles = useStyleConfig("Button", {
16-
...props,
17-
isSecondary,
18-
})
7+
export interface IProps extends ILinkProps, Omit<IButtonProps, "toId"> {}
198

20-
return (
21-
<Button
22-
as={BaseLink}
23-
activeStyle={{}}
24-
// `styles` object sent to `sx` prop per convention
25-
sx={{
26-
...styles,
27-
textDecoration: "none",
28-
_hover: { ...styles["_hover"], textDecoration: "none" },
29-
_visited: { color: styles["color"] },
30-
}}
31-
fontWeight="normal"
32-
{...props}
33-
>
34-
{children}
35-
</Button>
36-
)
9+
const ButtonLink: React.FC<IProps> = (props) => {
10+
return <Button as={BaseLink} activeStyle={{}} {...props} />
3711
}
3812

3913
export default ButtonLink

src/components/IconButton.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import * as React from "react"
2+
import {
3+
IconButton as ChakraIconButton,
4+
IconButtonProps as ChakraIconButtonProps,
5+
} from "@chakra-ui/react"
6+
import { checkIsSecondary, IProps as IButtonProps } from "./Button"
7+
8+
interface IconButtonProps
9+
extends Omit<IButtonProps, keyof ChakraIconButtonProps>,
10+
ChakraIconButtonProps {}
11+
12+
const IconButton = (props: IconButtonProps) => {
13+
const { isSecondary, ...rest } = props
14+
return (
15+
<ChakraIconButton
16+
{...checkIsSecondary({ variant: rest.variant?.toString(), isSecondary })}
17+
{...rest}
18+
/>
19+
)
20+
}
21+
22+
export default IconButton

src/components/Nav/Menu.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import React from "react"
22
import { useI18next } from "gatsby-plugin-react-i18next"
3-
import { Flex, List } from "@chakra-ui/react"
3+
import { Flex, FlexProps, List } from "@chakra-ui/react"
44

55
import NavDropdown from "./Dropdown"
66
import { getDirection } from "../../utils/translations"
77

88
import { Lang } from "../../utils/languages"
99
import { ISections } from "./types"
1010

11-
export interface IProps {
11+
export interface IProps extends FlexProps {
1212
path: string
1313
sections: ISections
1414
}
1515

16-
const Menu: React.FC<IProps> = ({ path, sections }) => {
16+
const Menu: React.FC<IProps> = ({ path, sections, ...props }) => {
1717
const { language } = useI18next()
1818
const direction = getDirection(language as Lang)
1919
const shouldShowSubNav = path.includes("/developers/")
@@ -23,7 +23,13 @@ const Menu: React.FC<IProps> = ({ path, sections }) => {
2323
const [start, basics, protocol] = learn.items
2424

2525
return (
26-
<Flex as={List} alignItems="center" m={0} gap={{ base: 3, xl: 6 }}>
26+
<Flex
27+
as={List}
28+
alignItems="center"
29+
m={0}
30+
gap={{ base: 3, xl: 6 }}
31+
{...props}
32+
>
2733
<NavDropdown section={useEthereum} hasSubNav={shouldShowSubNav}>
2834
{useEthereum.items.map((item, index) => (
2935
<NavDropdown.Item

0 commit comments

Comments
 (0)