From 1f567a14b75b2c669722013c98cbb9fbdb9ae690 Mon Sep 17 00:00:00 2001 From: v1rtl Date: Wed, 10 Apr 2024 17:28:19 +0300 Subject: [PATCH 1/2] fix links for mobile dropdown, remove 'as' --- .../molecules/Dropdown/ActionSheet.tsx | 25 ++++++++++++---- .../molecules/Dropdown/Dropdown.tsx | 30 +++++++++---------- .../reference/mdx/molecules/Dropdown.docs.mdx | 27 ++++++++++++++++- 3 files changed, 59 insertions(+), 23 deletions(-) diff --git a/components/src/components/molecules/Dropdown/ActionSheet.tsx b/components/src/components/molecules/Dropdown/ActionSheet.tsx index fc3959f9..679e1824 100644 --- a/components/src/components/molecules/Dropdown/ActionSheet.tsx +++ b/components/src/components/molecules/Dropdown/ActionSheet.tsx @@ -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' @@ -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 @@ -79,19 +86,25 @@ export const ActionSheet = React.forwardRef( return DropdownChild({ item, setIsOpen }) } - const icon = (item as DropdownItemObject).icon + const { icon, label, onClick, value, href, color } = + item as DropdownItemObject + return ( { - ;(item as DropdownItemObject)?.onClick?.( - (item as DropdownItemObject).value, - ) + onClick?.(value) setIsOpen(false) }} > {icon} - {(item as DropdownItemObject).label} + {href ? ( + + {label} + + ) : ( + {label} + )} ) })} diff --git a/components/src/components/molecules/Dropdown/Dropdown.tsx b/components/src/components/molecules/Dropdown/Dropdown.tsx index 6536806f..97138ddd 100644 --- a/components/src/components/molecules/Dropdown/Dropdown.tsx +++ b/components/src/components/molecules/Dropdown/Dropdown.tsx @@ -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 @@ -296,22 +295,21 @@ const DropdownMenu = React.forwardRef( label, onClick, disabled, - as, wrapper, showIndicator, href, } = item as DropdownItemObject - const props: React.ComponentProps = { + const props = { $hasColor: !!color, $color: color, $showIndicator: showIndicator, disabled, onClick: () => { - setIsOpen(false) onClick?.(value) + setIsOpen(false) }, - as, + as: href ? 'as' : 'button', children: ( <> {icon} @@ -319,7 +317,7 @@ const DropdownMenu = React.forwardRef( ), href, - } + } as const if (wrapper) { return wrapper(, value || label) @@ -479,18 +477,18 @@ const useScreenSize = () => { } const useClickOutside = ( - dropdownRef: React.MutableRefObject, - buttonRef: React.MutableRefObject, - actionSheetRef: React.MutableRefObject, + dropdownRef: React.MutableRefObject, + buttonRef: React.MutableRefObject, + actionSheetRef: React.MutableRefObject, setIsOpen: React.Dispatch>, 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) } @@ -527,9 +525,9 @@ export const Dropdown = ({ cancelLabel = 'Cancel', ...props }: Props & (PropsWithIsOpen | PropsWithoutIsOpen)) => { - const dropdownRef = React.useRef() - const buttonRef = React.useRef(null) - const actionSheetRef = React.useRef(null) + const dropdownRef = React.useRef(null) + const buttonRef = React.useRef(null) + const actionSheetRef = React.useRef(null) const internalOpen = React.useState(false) const [isOpen, setIsOpen] = _setIsOpen ? [_isOpen, _setIsOpen] : internalOpen diff --git a/docs/src/reference/mdx/molecules/Dropdown.docs.mdx b/docs/src/reference/mdx/molecules/Dropdown.docs.mdx index f85e69c7..cc7df3b6 100644 --- a/docs/src/reference/mdx/molecules/Dropdown.docs.mdx +++ b/docs/src/reference/mdx/molecules/Dropdown.docs.mdx @@ -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' + } } - }} /> @@ -296,3 +302,22 @@ import { Dropdown } from '@ensdomains/thorin' label="Custom" /> ``` + +## Items as link + +```tsx live=true minHeight=300px + +``` \ No newline at end of file From 12671ed8020950bcc02227ac79a8d2791db16292 Mon Sep 17 00:00:00 2001 From: v1rtl Date: Wed, 10 Apr 2024 17:33:26 +0300 Subject: [PATCH 2/2] newline --- docs/src/reference/mdx/molecules/Dropdown.docs.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/reference/mdx/molecules/Dropdown.docs.mdx b/docs/src/reference/mdx/molecules/Dropdown.docs.mdx index cc7df3b6..d202aeb1 100644 --- a/docs/src/reference/mdx/molecules/Dropdown.docs.mdx +++ b/docs/src/reference/mdx/molecules/Dropdown.docs.mdx @@ -320,4 +320,4 @@ import { Dropdown } from '@ensdomains/thorin' ]} label="Links" /> -``` \ No newline at end of file +```