diff --git a/.changeset/tricky-mugs-attend.md b/.changeset/tricky-mugs-attend.md new file mode 100644 index 00000000000..be8143084c6 --- /dev/null +++ b/.changeset/tricky-mugs-attend.md @@ -0,0 +1,5 @@ +--- +"@primer/components": patch +--- + +- Fixed Dropdown & Details types. diff --git a/.changeset/violet-oranges-speak.md b/.changeset/violet-oranges-speak.md new file mode 100644 index 00000000000..0f1d3f3ad47 --- /dev/null +++ b/.changeset/violet-oranges-speak.md @@ -0,0 +1,5 @@ +--- +"@primer/components": patch +--- + +- Added useDetails behavior back to Dropdown diff --git a/docs/content/Dropdown.md b/docs/content/Dropdown.md index 1254428aec0..48adbd83f85 100644 --- a/docs/content/Dropdown.md +++ b/docs/content/Dropdown.md @@ -34,31 +34,6 @@ Dropdown.Menu wraps your menu content. Be sure to pass a `direction` prop to thi ``` -## Manage the open state manually -The `Dropdown` element is built to also let you manage the open state and toggle functionality if necessary. Just provide values to the `open` and `onToggle` props. - -**Note:** Closing the dropdown on outside clicks will not function automatically if you chose to provide your own `open` state. You'll need to implement this yourself. You can use the `onClickOutside` prop to implement and customize this behavior. - -```jsx live - - {([open, setOpen]) => { - - const handleToggle = (e) => setOpen(e.target.open) - const handleClickOutside = () => setOpen(false) - - return ( - - Dropdown - - Item 1 - Item 2 - Item 3 - - - ) - }} - -``` ## System props @@ -66,17 +41,7 @@ Dropdown, Dropdown.Menu, Dropdown.Button, Dropdown.Caret, and Dropdown.Item all ## Component props -The Dropdown component is extended from the [`Details`](/Details) component and gets all props that the [`Details`](/Details) component gets. They are listed below, but you may reference the [`Details`](/Details) docs for more details on how to manage your own `open` state. - -#### Dropdown -| Name | Type | Default | Description | -| :- | :- | :-: | :- | -| defaultOpen | Boolean | | Sets the initial open/closed state | -| overlay | Boolean | false | Sets whether or not element will close when user clicks outside of it | -| open | Boolean | | Use the open prop if you'd like to manage the open state | -| onToggle | Function | | Called whenever user clicks on `summary` element. If you are controlling your own `open` state this will be the only function called on click, otherwise it's called before the internal `handleToggle` function.| -| onClickOutside | Function | | Function to call whenever user clicks outside of the Details component. This is optional and only necessary if you are controlling your own `open` state. | - +The Dropdown component is extended from the [`Details`](/Details) component and gets all props that the [`Details`](/Details) component gets. #### Dropdown.Menu diff --git a/index.d.ts b/index.d.ts index 21fff14e7dd..d30b1c7c61c 100644 --- a/index.d.ts +++ b/index.d.ts @@ -76,9 +76,7 @@ declare module '@primer/components' { export interface DetailsProps extends CommonProps, Omit, 'color'> { - onToggle: (event: React.SyntheticEvent) => void - open: boolean - ref: React.RefObject + ref?: React.RefObject } export const Details: React.FunctionComponent @@ -184,7 +182,6 @@ declare module '@primer/components' { export const StyledOcticon: React.FunctionComponent - export interface DropdownProps extends DetailsProps {} export interface DropdownItemProps extends CommonProps, Omit, 'color'> {} export interface DropdownMenuProps extends CommonProps, Omit, 'color'> { @@ -195,7 +192,7 @@ declare module '@primer/components' { export interface DropdownCaretProps extends CommonProps, Omit, 'color'> {} - export const Dropdown: React.FunctionComponent & { + export const Dropdown: React.FunctionComponent & { Menu: React.FunctionComponent Item: React.FunctionComponent Button: React.FunctionComponent diff --git a/src/Details.tsx b/src/Details.tsx index aa59f5ec6b9..37ad3c8db86 100644 --- a/src/Details.tsx +++ b/src/Details.tsx @@ -4,10 +4,7 @@ import {COMMON, SystemCommonProps} from './constants' import {ComponentProps} from './utils/types' import sx, {SxProp} from './sx' -type StyledDetailsProps = { - open?: boolean -} & SystemCommonProps & - SxProp +type StyledDetailsProps = SystemCommonProps & SxProp const Details = styled.details` & > summary { diff --git a/src/Dropdown.js b/src/Dropdown.js index ba9b920c013..2a033cdf52a 100644 --- a/src/Dropdown.js +++ b/src/Dropdown.js @@ -3,6 +3,7 @@ import PropTypes from 'prop-types' import styled from 'styled-components' import Button from './Button' import Details from './Details' +import useDetails from './hooks/useDetails' import {COMMON, get} from './constants' import getDirectionStyles from './DropdownStyles' import theme from './theme' @@ -14,8 +15,9 @@ const StyledDetails = styled(Details)` ` const Dropdown = ({children, className, ...rest}) => { + const {getDetailsProps} = useDetails({closeOnOutsideClick: true}) return ( - + {children} ) diff --git a/src/__tests__/__snapshots__/Dropdown.js.snap b/src/__tests__/__snapshots__/Dropdown.js.snap index a17dfe8f19a..731fb758646 100644 --- a/src/__tests__/__snapshots__/Dropdown.js.snap +++ b/src/__tests__/__snapshots__/Dropdown.js.snap @@ -16,6 +16,7 @@ exports[`Dropdown renders consistently 1`] = `
Hello!