Skip to content

Commit

Permalink
Merge pull request #144 from ensdomains/dropdown-mobile-fix
Browse files Browse the repository at this point in the history
fix(Dropdown): links for mobile dropdown, remove 'as' from props
  • Loading branch information
talentlessguy authored Apr 10, 2024
2 parents 092c952 + 12671ed commit 10e5c6c
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 23 deletions.
25 changes: 19 additions & 6 deletions components/src/components/molecules/Dropdown/ActionSheet.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react'
import styled, { css } from 'styled-components'

import { breakpoints } from '@/src/tokens'
import { Colors, breakpoints } from '@/src/tokens'

import { Button, Modal, Typography } from '../..'
import type { DropdownItem, DropdownItemObject } from './Dropdown'
Expand Down Expand Up @@ -49,6 +49,13 @@ const ActionSheetItem = styled.div(
`,
)

const ActionSheetLinkItem = styled.a<{ $color?: Colors }>(
({ theme, $color = 'text' }) => css`
color: ${theme.colors[$color]};
font-weight: ${theme.fontWeights.normal};
`,
)

type Props = {
isOpen: boolean
screenSize: number
Expand Down Expand Up @@ -79,19 +86,25 @@ export const ActionSheet = React.forwardRef<HTMLDivElement, Props>(
return DropdownChild({ item, setIsOpen })
}

const icon = (item as DropdownItemObject).icon
const { icon, label, onClick, value, href, color } =
item as DropdownItemObject

return (
<ActionSheetItem
key={(item as DropdownItemObject).label}
onClick={() => {
;(item as DropdownItemObject)?.onClick?.(
(item as DropdownItemObject).value,
)
onClick?.(value)
setIsOpen(false)
}}
>
{icon}
<Typography>{(item as DropdownItemObject).label}</Typography>
{href ? (
<ActionSheetLinkItem $color={color} href={href}>
{label}
</ActionSheetLinkItem>
) : (
<Typography color={color}>{label}</Typography>
)}
</ActionSheetItem>
)
})}
Expand Down
30 changes: 14 additions & 16 deletions components/src/components/molecules/Dropdown/Dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export type DropdownItemObject = {
label: string
onClick?: (value?: string) => void
wrapper?: (children: React.ReactNode, key: React.Key) => JSX.Element
as?: 'button' | 'a'
icon?: React.ReactNode
value?: string
color?: Colors
Expand Down Expand Up @@ -296,30 +295,29 @@ const DropdownMenu = React.forwardRef<HTMLDivElement, DropdownMenuProps>(
label,
onClick,
disabled,
as,
wrapper,
showIndicator,
href,
} = item as DropdownItemObject

const props: React.ComponentProps<any> = {
const props = {
$hasColor: !!color,
$color: color,
$showIndicator: showIndicator,
disabled,
onClick: () => {
setIsOpen(false)
onClick?.(value)
setIsOpen(false)
},
as,
as: href ? 'as' : 'button',
children: (
<>
{icon}
{label}
</>
),
href,
}
} as const

if (wrapper) {
return wrapper(<MenuButton {...props} type="button" />, value || label)
Expand Down Expand Up @@ -479,18 +477,18 @@ const useScreenSize = () => {
}

const useClickOutside = (
dropdownRef: React.MutableRefObject<any>,
buttonRef: React.MutableRefObject<any>,
actionSheetRef: React.MutableRefObject<any>,
dropdownRef: React.MutableRefObject<HTMLElement | null>,
buttonRef: React.MutableRefObject<HTMLButtonElement | null>,
actionSheetRef: React.MutableRefObject<HTMLDivElement | null>,
setIsOpen: React.Dispatch<React.SetStateAction<boolean>>,
isOpen: boolean,
) => {
React.useEffect(() => {
const handleClickOutside = (e: any) => {
const handleClickOutside = (e: MouseEvent) => {
if (
!dropdownRef.current?.contains(e.target) &&
!buttonRef.current?.contains(e.target) &&
!actionSheetRef.current?.contains(e.target)
!dropdownRef.current?.contains(e.target as Node) &&
!buttonRef.current?.contains(e.target as Node) &&
!actionSheetRef.current?.contains(e.target as Node)
) {
setIsOpen(false)
}
Expand Down Expand Up @@ -527,9 +525,9 @@ export const Dropdown = ({
cancelLabel = 'Cancel',
...props
}: Props & (PropsWithIsOpen | PropsWithoutIsOpen)) => {
const dropdownRef = React.useRef<any>()
const buttonRef = React.useRef<HTMLButtonElement>(null)
const actionSheetRef = React.useRef<HTMLDivElement>(null)
const dropdownRef = React.useRef<HTMLDivElement | null>(null)
const buttonRef = React.useRef<HTMLButtonElement | null>(null)
const actionSheetRef = React.useRef<HTMLDivElement | null>(null)
const internalOpen = React.useState(false)
const [isOpen, setIsOpen] = _setIsOpen ? [_isOpen, _setIsOpen] : internalOpen

Expand Down
27 changes: 26 additions & 1 deletion docs/src/reference/mdx/molecules/Dropdown.docs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,14 @@ import { Dropdown } from '@ensdomains/thorin'
type: {
raw: 'ReactNode'
}
},
href: {
name: 'href',
description: 'A link that opens on click.',
type: {
raw: 'string'
}
}

}}
/>

Expand Down Expand Up @@ -296,3 +302,22 @@ import { Dropdown } from '@ensdomains/thorin'
label="Custom"
/>
```

## Items as link

```tsx live=true minHeight=300px
<Dropdown
items={[
{
label: 'I am a link',
href: 'https://ens.domains',
color: 'text'
},
{
label: 'I am not a link',
color: 'text'
}
]}
label="Links"
/>
```

0 comments on commit 10e5c6c

Please sign in to comment.