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;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EA8CF;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;;AA5QH;EAAA;;;;EAAA;;;;;AAAA;EAAA;;;;;AAAA;EAAA;;;;;AAAA;EAAA;;;;;AAAA;EAAA;;;;;AAAA;EAAA;;;;;AAAA;EAAA;;;;;AAAA;EAAA;IAAA;;;;IAsFC;;;;;;AAtFD;EAAA;IAAA;;;;IAAA;;;;IA2BK;;;;IAAA;;;;IAmMA","sources":["56ed516746d0036e","packages/@react-spectrum/s2/src/TreeView.tsx"],"sourcesContent":["@import \"9f0cb543acce7895\";\n@import \"1efe6bd9df94f465\";\n@import \"bdf071bf66df1ce3\";\n@import \"0330b2193d88b3df\";\n@import \"888ae06603582201\";\n@import \"73010de75c5ff12a\";\n@import \"6f9cf7ba5fc61169\";\n@import \"e3d8cbff54f98abf\";\n@import \"5ce50fd84b82affa\";\n@import \"e7f15e944ad73835\";\n@import \"17e37b578317afa7\";\n@import \"664b85d9761c64e2\";\n@import \"b6201a1b57a21b4e\";\n@import \"73100caf88a76f92\";\n","/*\n * Copyright 2024 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {ActionButtonGroupContext} from './ActionButtonGroup';\nimport {ActionMenuContext} from './ActionMenu';\nimport {\n Button,\n ButtonContext,\n ListLayout,\n Provider,\n TreeItemProps as RACTreeItemProps,\n TreeProps as RACTreeProps,\n Tree,\n TreeItem,\n TreeItemContent,\n TreeItemContentProps,\n useContextProps,\n Virtualizer\n} from 'react-aria-components';\nimport {centerBaseline} from './CenterBaseline';\nimport {Checkbox} from './Checkbox';\nimport Chevron from '../ui-icons/Chevron';\nimport {colorMix, focusRing, fontRelative, lightDark, style} from '../style' with {type: 'macro'};\nimport {DOMRef, Key} from '@react-types/shared';\nimport {getAllowedOverrides, StylesPropWithHeight, UnsafeStyles} from './style-utils' with {type: 'macro'};\nimport {IconContext} from './Icon';\nimport {raw} from '../style/style-macro' with {type: 'macro'};\nimport React, {createContext, forwardRef, JSXElementConstructor, ReactElement, ReactNode, useContext, useRef} from 'react';\nimport {TextContext} from './Content';\nimport {useDOMRef} from '@react-spectrum/utils';\nimport {useLocale} from 'react-aria';\nimport {useScale} from './utils';\n\ninterface S2TreeProps {\n // Only detatched is supported right now with the current styles from Spectrum\n isDetached?: boolean,\n onAction?: (key: Key) => void,\n // not fully supported yet\n isEmphasized?: boolean\n}\n\nexport interface TreeViewProps extends Omit<RACTreeProps<any>, 'style' | 'className' | 'onRowAction' | 'selectionBehavior' | 'onScroll' | 'onCellAction' | 'dragAndDropHooks'>, UnsafeStyles, S2TreeProps {\n /** Spectrum-defined styles, returned by the `style()` macro. */\n styles?: StylesPropWithHeight\n}\n\nexport interface TreeViewItemProps extends Omit<RACTreeItemProps, 'className' | 'style'> {\n /** Whether this item has children, even if not loaded yet. */\n hasChildItems?: boolean\n}\n\ninterface TreeRendererContextValue {\n renderer?: (item) => ReactElement<any, string | JSXElementConstructor<any>>\n}\nconst TreeRendererContext = createContext<TreeRendererContextValue>({});\n\n\nlet InternalTreeContext = createContext<{isDetached?: boolean, isEmphasized?: boolean}>({});\n\n// TODO: the below is needed so the borders of the top and bottom row isn't cut off if the TreeView is wrapped within a container by always reserving the 2px needed for the\n// keyboard focus ring. Perhaps find a different way of rendering the outlines since the top of the item doesn't\n// scroll into view due to how the ring is offset. Alternatively, have the tree render the top/bottom outline like it does in Listview\nconst tree = style({\n ...focusRing(),\n outlineOffset: -2, // make certain we are visible inside overflow hidden containers\n userSelect: 'none',\n minHeight: 0,\n minWidth: 0,\n width: 'full',\n height: 'full',\n overflow: 'auto',\n boxSizing: 'border-box',\n justifyContent: {\n isEmpty: 'center'\n },\n alignItems: {\n isEmpty: 'center'\n },\n '--indent': {\n type: 'width',\n value: 16\n }\n}, getAllowedOverrides({height: true}));\n\nfunction TreeView(props: TreeViewProps, ref: DOMRef<HTMLDivElement>) {\n let {children, isDetached, isEmphasized, UNSAFE_className, UNSAFE_style} = props;\n let scale = useScale();\n\n let renderer;\n if (typeof children === 'function') {\n renderer = children;\n }\n\n let domRef = useDOMRef(ref);\n\n return (\n <Virtualizer\n layout={ListLayout}\n layoutOptions={{\n rowHeight: scale === 'large' ? 50 : 40,\n gap: isDetached ? 2 : 0\n }}>\n <TreeRendererContext.Provider value={{renderer}}>\n <InternalTreeContext.Provider value={{isDetached, isEmphasized}}>\n <Tree\n {...props}\n style={UNSAFE_style}\n className={renderProps => (UNSAFE_className ?? '') + tree({isDetached, ...renderProps}, props.styles)}\n selectionBehavior=\"toggle\"\n ref={domRef}>\n {props.children}\n </Tree>\n </InternalTreeContext.Provider>\n </TreeRendererContext.Provider>\n </Virtualizer>\n );\n}\n\nconst selectedBackground = lightDark(colorMix('gray-25', 'informative-900', 10), colorMix('gray-25', 'informative-700', 10));\nconst selectedActiveBackground = lightDark(colorMix('gray-25', 'informative-900', 15), colorMix('gray-25', 'informative-700', 15));\n\nconst rowBackgroundColor = {\n default: '--s2-container-bg',\n isFocusVisibleWithin: colorMix('gray-25', 'gray-900', 7),\n isHovered: colorMix('gray-25', 'gray-900', 7),\n isPressed: colorMix('gray-25', 'gray-900', 10),\n isSelected: {\n default: colorMix('gray-25', 'gray-900', 7),\n isEmphasized: selectedBackground,\n isFocusVisibleWithin: {\n default: colorMix('gray-25', 'gray-900', 10),\n isEmphasized: selectedActiveBackground\n },\n isHovered: {\n default: colorMix('gray-25', 'gray-900', 10),\n isEmphasized: selectedActiveBackground\n },\n isPressed: {\n default: colorMix('gray-25', 'gray-900', 10),\n isEmphasized: selectedActiveBackground\n }\n },\n forcedColors: {\n default: 'Background'\n }\n} as const;\n\nconst treeRow = style({\n position: 'relative',\n display: 'flex',\n height: 40,\n width: 'full',\n boxSizing: 'border-box',\n font: 'ui',\n color: 'body',\n outlineStyle: 'none',\n cursor: {\n default: 'default',\n isLink: 'pointer'\n },\n '--rowBackgroundColor': {\n type: 'backgroundColor',\n value: rowBackgroundColor\n },\n '--rowFocusIndicatorColor': {\n type: 'outlineColor',\n value: {\n default: 'focus-ring',\n forcedColors: 'Highlight'\n }\n }\n});\n\n\nconst treeCellGrid = style({\n display: 'grid',\n width: 'full',\n height: 'full',\n boxSizing: 'border-box',\n alignContent: 'center',\n alignItems: 'center',\n gridTemplateColumns: ['auto', 'auto', 'auto', 'auto', 'auto', '1fr', 'minmax(0, auto)', 'auto'],\n gridTemplateRows: '1fr',\n gridTemplateAreas: [\n 'drag-handle checkbox level-padding expand-button icon content actions actionmenu'\n ],\n backgroundColor: '--rowBackgroundColor',\n paddingEnd: 4, // account for any focus rings on the last item in the cell\n color: {\n isDisabled: {\n default: 'gray-400',\n forcedColors: 'GrayText'\n }\n },\n '--rowSelectedBorderColor': {\n type: 'outlineColor',\n value: {\n default: 'gray-800',\n isFocusVisible: 'focus-ring',\n forcedColors: 'Highlight'\n }\n },\n '--rowForcedFocusBorderColor': {\n type: 'outlineColor',\n value: {\n default: 'focus-ring',\n forcedColors: 'Highlight'\n }\n },\n borderColor: {\n isDetached: {\n default: 'transparent',\n isSelected: '--rowSelectedBorderColor'\n }\n },\n borderWidth: {\n isDetached: 1\n },\n borderRadius: {\n isDetached: 'default'\n },\n borderStyle: {\n isDetached: 'solid'\n }\n});\n\nconst treeCheckbox = style({\n gridArea: 'checkbox',\n marginStart: 12,\n marginEnd: 0,\n paddingEnd: 0\n});\n\nconst treeIcon = style({\n gridArea: 'icon',\n marginEnd: 'text-to-visual',\n '--iconPrimary': {\n type: 'fill',\n value: 'currentColor'\n }\n});\n\nconst treeContent = style({\n gridArea: 'content',\n textOverflow: 'ellipsis',\n whiteSpace: 'nowrap',\n overflow: 'hidden'\n});\n\nconst treeActions = style({\n gridArea: 'actions',\n /* TODO: I made this one up, confirm desired behavior. These paddings are to make sure the action group has enough padding for the focus ring */\n marginStart: 2,\n marginEnd: 4\n});\n\nconst treeActionMenu = style({\n gridArea: 'actionmenu'\n});\n\nconst cellFocus = {\n outlineStyle: {\n default: 'none',\n isFocusVisible: 'solid'\n },\n outlineOffset: -2,\n outlineWidth: 2,\n outlineColor: 'focus-ring',\n borderRadius: '[6px]'\n} as const;\n\nconst treeRowFocusIndicator = raw(`\n &:before {\n content: \"\";\n display: inline-block;\n position: sticky;\n inset-inline-start: 0;\n width: 3px;\n height: 100%;\n margin-inline-end: -3px;\n margin-block-end: 1px;\n z-index: 3;\n background-color: var(--rowFocusIndicatorColor);\n }`\n);\n\nexport const TreeViewItem = (props: TreeViewItemProps): ReactNode => {\n let {\n href\n } = props;\n let {isDetached, isEmphasized} = useContext(InternalTreeContext);\n\n return (\n <TreeItem\n {...props}\n className={(renderProps) => treeRow({\n ...renderProps,\n isLink: !!href, isEmphasized\n }) + (renderProps.isFocusVisible && !isDetached ? ' ' + treeRowFocusIndicator : '')} />\n );\n};\n\nexport interface TreeViewItemContentProps extends Omit<TreeItemContentProps, 'children'> {\n /** Rendered contents of the tree item or child items. */\n children: ReactNode\n}\n\nexport const TreeViewItemContent = (props: TreeViewItemContentProps): ReactNode => {\n let {\n children\n } = props;\n let {isDetached, isEmphasized} = useContext(InternalTreeContext);\n let scale = useScale();\n\n return (\n <TreeItemContent>\n {({isExpanded, hasChildItems, selectionMode, selectionBehavior, isDisabled, isFocusVisible, isSelected, id, state}) => {\n let isNextSelected = false;\n let isNextFocused = false;\n let keyAfter = state.collection.getKeyAfter(id);\n if (keyAfter != null) {\n isNextSelected = state.selectionManager.isSelected(keyAfter);\n }\n let isFirst = state.collection.getFirstKey() === id;\n return (\n <div className={treeCellGrid({isDisabled, isNextSelected, isSelected, isFirst, isNextFocused, isDetached})}>\n {selectionMode !== 'none' && selectionBehavior === 'toggle' && (\n // TODO: add transition?\n <div className={treeCheckbox}>\n <Checkbox\n isEmphasized={isEmphasized}\n slot=\"selection\" />\n </div>\n )}\n <div\n className={style({\n gridArea: 'level-padding',\n width: 'calc(calc(var(--tree-item-level, 0) - 1) * var(--indent))'\n })} />\n {/* TODO: revisit when we do async loading, at the moment hasChildItems will only cause the chevron to be rendered, no aria/data attributes indicating the row's expandability are added */}\n <ExpandableRowChevron isDisabled={isDisabled} isExpanded={isExpanded} scale={scale} isHidden={!(hasChildItems)} />\n <Provider\n values={[\n [TextContext, {styles: treeContent}],\n [IconContext, {\n render: centerBaseline({slot: 'icon', styles: treeIcon}),\n styles: style({size: fontRelative(20), flexShrink: 0})\n }],\n [ActionButtonGroupContext, {styles: treeActions}],\n [ActionMenuContext, {styles: treeActionMenu, isQuiet: true}]\n ]}>\n {children}\n </Provider>\n {isFocusVisible && isDetached && <div role=\"presentation\" className={style({...cellFocus, position: 'absolute', inset: 0})({isFocusVisible: true})} />}\n </div>\n );\n }}\n </TreeItemContent>\n );\n};\n\ninterface ExpandableRowChevronProps {\n isExpanded?: boolean,\n isDisabled?: boolean,\n isRTL?: boolean,\n scale: 'medium' | 'large',\n isHidden?: boolean\n}\n\nconst expandButton = style<ExpandableRowChevronProps>({\n gridArea: 'expand-button',\n color: {\n default: 'inherit',\n isDisabled: {\n default: 'disabled',\n forcedColors: 'GrayText'\n }\n },\n height: 40,\n width: 40,\n display: 'flex',\n flexWrap: 'wrap',\n alignContent: 'center',\n justifyContent: 'center',\n outlineStyle: 'none',\n cursor: 'default',\n transform: {\n isExpanded: {\n default: 'rotate(90deg)',\n isRTL: 'rotate(-90deg)'\n }\n },\n padding: 0,\n transition: 'default',\n backgroundColor: 'transparent',\n borderStyle: 'none',\n disableTapHighlight: true,\n visibility: {\n isHidden: 'hidden'\n }\n});\n\nfunction ExpandableRowChevron(props: ExpandableRowChevronProps) {\n let expandButtonRef = useRef<HTMLButtonElement>(null);\n let [fullProps, ref] = useContextProps({...props, slot: 'chevron'}, expandButtonRef, ButtonContext);\n let {isExpanded, scale, isHidden} = fullProps;\n let {direction} = useLocale();\n\n return (\n <Button\n {...props}\n ref={ref}\n slot=\"chevron\"\n className={renderProps => expandButton({...renderProps, isExpanded, isRTL: direction === 'rtl', scale, isHidden})}>\n <Chevron\n className={style({\n scale: {\n direction: {\n ltr: '1',\n rtl: '-1'\n }\n },\n '--iconPrimary': {\n type: 'fill',\n value: 'currentColor'\n }\n })({direction})} />\n </Button>\n );\n}\n\n/**\n * A tree view provides users with a way to navigate nested hierarchical information.\n */\nconst _TreeView = forwardRef(TreeView);\nexport {_TreeView as TreeView};\n"],"names":[],"version":3,"file":"TreeView.css.map"}