flowbite-react
Version:
Official React components built for Flowbite and Tailwind CSS
1 lines • 13.3 kB
Source Map (JSON)
{"version":3,"file":"Dropdown.cjs","sources":["../../../src/components/Dropdown/Dropdown.tsx"],"sourcesContent":["\"use client\";\n\nimport type { ExtendedRefs } from \"@floating-ui/react\";\nimport { FloatingFocusManager, FloatingList, useListNavigation, useTypeahead } from \"@floating-ui/react\";\nimport type {\n ComponentProps,\n Dispatch,\n FC,\n HTMLProps,\n MutableRefObject,\n ReactElement,\n ReactNode,\n SetStateAction,\n} from \"react\";\nimport { cloneElement, useCallback, useEffect, useMemo, useRef, useState } from \"react\";\nimport { get } from \"../../helpers/get\";\nimport { resolveProps } from \"../../helpers/resolve-props\";\nimport { useResolveTheme } from \"../../helpers/resolve-theme\";\nimport { twMerge } from \"../../helpers/tailwind-merge\";\nimport { useBaseFLoating, useFloatingInteractions } from \"../../hooks/use-floating\";\nimport { ChevronDownIcon } from \"../../icons/chevron-down-icon\";\nimport { ChevronLeftIcon } from \"../../icons/chevron-left-icon\";\nimport { ChevronRightIcon } from \"../../icons/chevron-right-icon\";\nimport { ChevronUpIcon } from \"../../icons/chevron-up-icon\";\nimport { useThemeProvider } from \"../../theme/provider\";\nimport type { ThemingProps } from \"../../types\";\nimport { Button, type ButtonProps } from \"../Button\";\nimport type { FloatingProps, FloatingTheme } from \"../Floating\";\nimport { DropdownContext } from \"./DropdownContext\";\nimport type { DropdownDividerTheme } from \"./DropdownDivider\";\nimport type { DropdownHeaderTheme } from \"./DropdownHeader\";\nimport type { DropdownItemTheme } from \"./DropdownItem\";\nimport { dropdownTheme } from \"./theme\";\n\nexport interface DropdownFloatingTheme extends FloatingTheme, DropdownDividerTheme, DropdownHeaderTheme {\n item: DropdownItemTheme;\n}\n\nexport interface DropdownTheme {\n floating: DropdownFloatingTheme;\n content: string;\n inlineWrapper: string;\n arrowIcon: string;\n}\n\nexport interface DropdownProps\n extends Pick<FloatingProps, \"placement\" | \"trigger\">,\n Omit<ButtonProps, keyof ThemingProps<DropdownTheme>>,\n ThemingProps<DropdownTheme> {\n arrowIcon?: boolean;\n dismissOnClick?: boolean;\n floatingArrow?: boolean;\n inline?: boolean;\n label?: ReactNode;\n enableTypeAhead?: boolean;\n renderTrigger?: (theme: DropdownTheme) => ReactElement;\n \"data-testid\"?: string;\n}\n\nconst icons: Record<string, FC<ComponentProps<\"svg\">>> = {\n top: ChevronUpIcon,\n right: ChevronRightIcon,\n bottom: ChevronDownIcon,\n left: ChevronLeftIcon,\n};\n\nexport interface TriggerProps extends Omit<ButtonProps, keyof ThemingProps<DropdownTheme>> {\n refs: ExtendedRefs<HTMLElement>;\n inline?: boolean;\n theme: DropdownTheme;\n setButtonWidth?: Dispatch<SetStateAction<number | undefined>>;\n getReferenceProps: (userProps?: HTMLProps<Element> | undefined) => Record<string, unknown>;\n renderTrigger?: (theme: DropdownTheme) => ReactElement;\n}\n\nfunction Trigger({\n refs,\n children,\n inline,\n theme,\n disabled,\n setButtonWidth,\n getReferenceProps,\n renderTrigger,\n ...buttonProps\n}: TriggerProps) {\n const ref = refs.reference as MutableRefObject<HTMLElement>;\n const a11yProps = getReferenceProps();\n\n useEffect(() => {\n if (ref.current) {\n setButtonWidth?.(ref.current.clientWidth);\n }\n }, [ref, setButtonWidth]);\n\n if (renderTrigger) {\n const triggerElement = renderTrigger(theme);\n return cloneElement(triggerElement, { ref: refs.setReference, disabled, ...a11yProps, ...triggerElement.props });\n }\n\n return inline ? (\n <button type=\"button\" ref={refs.setReference} className={theme?.inlineWrapper} disabled={disabled} {...a11yProps}>\n {children}\n </button>\n ) : (\n <Button {...buttonProps} disabled={disabled} type=\"button\" ref={refs.setReference} {...a11yProps}>\n {children}\n </Button>\n );\n}\n\nexport function Dropdown(props: DropdownProps) {\n const [open, setOpen] = useState(false);\n const [activeIndex, setActiveIndex] = useState<number | null>(null);\n const [selectedIndex, setSelectedIndex] = useState<number | null>(null);\n const [buttonWidth, setButtonWidth] = useState<number | undefined>(undefined);\n const elementsRef = useRef<Array<HTMLElement | null>>([]);\n const labelsRef = useRef<Array<string | null>>([]);\n\n const provider = useThemeProvider();\n const theme = useResolveTheme(\n [dropdownTheme, provider.theme?.dropdown, props.theme],\n [get(provider.clearTheme, \"dropdown\"), props.clearTheme],\n [get(provider.applyTheme, \"dropdown\"), props.applyTheme],\n );\n\n const {\n children,\n className,\n dismissOnClick = true,\n enableTypeAhead = true,\n renderTrigger,\n ...restProps\n } = resolveProps(props, provider.props?.dropdown);\n\n const {\n placement = restProps.inline ? \"bottom-start\" : \"bottom\",\n trigger = \"click\",\n label,\n inline,\n arrowIcon = true,\n ...buttonProps\n } = restProps;\n const dataTestId = restProps[\"data-testid\"] || \"flowbite-dropdown-target\";\n\n const handleSelect = useCallback((index: number | null) => {\n setSelectedIndex(index);\n setOpen(false);\n }, []);\n\n const handleTypeaheadMatch = useCallback(\n (index: number | null) => {\n if (open) {\n setActiveIndex(index);\n } else {\n handleSelect(index);\n }\n },\n [open, handleSelect],\n );\n\n const { context, floatingStyles, refs } = useBaseFLoating<HTMLButtonElement>({\n open,\n setOpen,\n placement,\n });\n\n const listNav = useListNavigation(context, {\n listRef: elementsRef,\n activeIndex,\n selectedIndex,\n onNavigate: setActiveIndex,\n });\n\n const typeahead = useTypeahead(context, {\n listRef: labelsRef,\n activeIndex,\n selectedIndex,\n onMatch: handleTypeaheadMatch,\n enabled: enableTypeAhead,\n });\n\n const { getReferenceProps, getFloatingProps, getItemProps } = useFloatingInteractions({\n context,\n role: \"menu\",\n trigger,\n interactions: [listNav, typeahead],\n });\n\n const Icon = useMemo(() => {\n const [p] = placement.split(\"-\");\n return icons[p] ?? ChevronDownIcon;\n }, [placement]);\n\n return (\n <DropdownContext.Provider\n value={{\n theme: props.theme,\n clearTheme: props.clearTheme,\n applyTheme: props.applyTheme,\n activeIndex,\n dismissOnClick,\n getItemProps,\n handleSelect,\n }}\n >\n <Trigger\n {...buttonProps}\n refs={refs}\n inline={inline}\n theme={theme}\n data-testid={dataTestId}\n className={twMerge(theme.floating.target, className)}\n setButtonWidth={setButtonWidth}\n getReferenceProps={getReferenceProps}\n renderTrigger={renderTrigger}\n >\n {label}\n {arrowIcon && <Icon className={theme.arrowIcon} />}\n </Trigger>\n {open && (\n <FloatingFocusManager context={context} modal={false}>\n <div\n ref={refs.setFloating}\n style={{ ...floatingStyles, minWidth: buttonWidth }}\n data-testid=\"flowbite-dropdown\"\n aria-expanded={open}\n {...getFloatingProps({\n className: twMerge(\n theme.floating.base,\n theme.floating.animation,\n \"duration-100\",\n !open && theme.floating.hidden,\n theme.floating.style.auto,\n className,\n ),\n })}\n >\n <FloatingList elementsRef={elementsRef} labelsRef={labelsRef}>\n <ul className={theme.content} tabIndex={-1}>\n {children}\n </ul>\n </FloatingList>\n </div>\n </FloatingFocusManager>\n )}\n </DropdownContext.Provider>\n );\n}\n\nDropdown.displayName = \"Dropdown\";\n"],"names":["ChevronUpIcon","ChevronRightIcon","ChevronDownIcon","ChevronLeftIcon","useEffect","cloneElement","jsx","Button","useState","useRef","provider","useThemeProvider","theme","useResolveTheme","dropdownTheme","get","resolveProps","useCallback","useBaseFLoating","useListNavigation","useTypeahead","useFloatingInteractions","useMemo","jsxs","DropdownContext","twMerge","FloatingFocusManager","FloatingList"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAkBA,MAAM,KAAK,GAAG;AACd,EAAE,GAAG,EAAEA,2BAAa;AACpB,EAAE,KAAK,EAAEC,iCAAgB;AACzB,EAAE,MAAM,EAAEC,+BAAe;AACzB,EAAE,IAAI,EAAEC;AACR,CAAC;AACD,SAAS,OAAO,CAAC;AACjB,EAAE,IAAI;AACN,EAAE,QAAQ;AACV,EAAE,MAAM;AACR,EAAE,KAAK;AACP,EAAE,QAAQ;AACV,EAAE,cAAc;AAChB,EAAE,iBAAiB;AACnB,EAAE,aAAa;AACf,EAAE,GAAG;AACL,CAAC,EAAE;AACH,EAAE,MAAM,GAAG,GAAG,IAAI,CAAC,SAAS;AAC5B,EAAE,MAAM,SAAS,GAAG,iBA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