react-box-overflow
Version:
Headless UI for automatically collapsing boxes when overflow in React.
1 lines • 7.11 kB
Source Map (JSON)
{"version":3,"file":"BoxOverflow.cjs","sources":["../../src/BoxOverflow.tsx"],"sourcesContent":["/**\n * @author shunzi <tobyzsj@gmail.com>\n * @date 2024-03-24 21:47:13\n */\nimport type { FunctionComponent } from 'react'\nimport React, { cloneElement, createElement, isValidElement, useContext, useMemo } from 'react'\nimport type { BoxOverflow as BoxOverflowInstance } from 'box-overflow-core'\nimport { useOverflow } from './useOverflow'\nimport type { DataType } from './types'\nimport type { BoxOverflowItemProps, BoxOverflowProps, BoxOverflowRestProps, CommonChildProps } from './interface'\nimport { capitalizeFirstLetter } from './utils'\n\nconst BoxOverflowContext = React.createContext<BoxOverflowInstance >({} as BoxOverflowInstance)\n\nconst fixedDisplayNameMap = {\n rest: 'BoxOverflowRest',\n prefix: 'BoxOverflowPrefix',\n suffix: 'BoxOverflowSuffix',\n}\n\ntype FixKeys = keyof typeof fixedDisplayNameMap\n\nfunction genFixComponent(type: 'prefix' | 'suffix'): FunctionComponent<CommonChildProps> {\n const Comp = (props: CommonChildProps) => {\n const { component = 'div', ...restProps } = props\n const instance = useContext(BoxOverflowContext)\n const idKey = instance.options?.idAttribute as string\n\n return createElement(component, { ...restProps, [idKey]: type })\n }\n Comp.displayName = `BoxOverflow${capitalizeFirstLetter(type)}`\n return Comp\n}\n\nconst BoxOverflowItem: FunctionComponent<BoxOverflowItemProps> = (props: BoxOverflowItemProps) => {\n const { component = 'div', id: _id, ...restProps } = props\n const instance = useContext(BoxOverflowContext)\n const idKey = instance.options?.idAttribute as string\n\n const id = String(_id)\n\n const style = {\n ...props.style,\n ...instance.getItemStyle(id),\n }\n return createElement(component, { ...restProps, [idKey]: id, style })\n}\nBoxOverflowItem.displayName = 'BoxOverflowItem'\n\nconst BoxOverflowRest: FunctionComponent<BoxOverflowRestProps> = (props: BoxOverflowRestProps) => {\n const { component = 'div', children: _children, ...restProps } = props\n const instance = useContext(BoxOverflowContext)\n const idKey = instance.options?.idAttribute as string\n const style = instance.getRestStyle()\n\n const rests = instance.getRests()\n\n const children = useMemo (() => {\n if (typeof _children === 'function')\n return _children(rests)\n return _children\n }, [_children, rests])\n\n return createElement(component, { ...restProps, ...props, [idKey]: 'rest', children, style: { ...props.style, ...style } })\n}\nBoxOverflowRest.displayName = 'BoxOverflowRest'\n\nexport function BoxOverflow<K extends keyof any = 'key', D extends DataType<K> = DataType<K>>(props: BoxOverflowProps<K, D>) {\n const { component, children, className, style, ...restProps } = props\n\n const containerRef = React.useRef<HTMLDivElement>(null)\n\n const keyList: string[] = []\n const _children: React.ReactNode[] = []\n const fixItems: Partial<Record<FixKeys, React.ReactElement | null>> = { }\n\n React.Children.forEach(children, (child, index) => {\n if (isValidElement(child)) {\n const displayName = (child.type as unknown as { displayName: string }).displayName\n\n if (displayName === 'BoxOverflowItem') {\n const id = child.props.id ?? child.key ?? index\n const idStr = String(id)\n _children.push(cloneElement (child, { key: idStr, id: idStr, ...child.props }))\n keyList.push(idStr)\n }\n\n const type = Object.entries(fixedDisplayNameMap).find(([_, value]) => value === displayName)?.[0]\n if (type)\n fixItems[type as FixKeys] = cloneElement(child, { key: type })\n }\n })\n\n const instance = useOverflow({\n getIdByIndex: (index: number) => {\n return keyList[index]\n },\n ...restProps,\n getContainer: () => containerRef.current as HTMLDivElement,\n })\n\n const _style: React.CSSProperties = {\n ...instance.getContainerStyle() as React.CSSProperties,\n ...style,\n }\n\n const _component = component || 'div'\n return (\n <BoxOverflowContext.Provider value={instance}>\n {React.createElement(_component, {\n children: [fixItems.prefix, ..._children, fixItems.rest, fixItems.suffix],\n ref: containerRef,\n className,\n style: _style,\n })}\n </BoxOverflowContext.Provider>\n )\n}\nBoxOverflow.displayName = 'BoxOverflow'\nBoxOverflow.Rest = BoxOverflowRest\nBoxOverflow.Item = BoxOverflowItem\nBoxOverflow.Suffix = genFixComponent('suffix')\nBoxOverflow.Prefix = genFixComponent('prefix')\n"],"names":["useContext","createElement","cap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