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cAAA,gCAAC,CAAA,GAAA,mCAAW;wBACV,KAAK;wBACJ,GAAG,UAAU;wBACd,OAAO;wBACP,QAAO;wBACP,mBAAmB,mBAAmB,cAAc,YAAY;wBAChE,cAAc;wBACd,qBAAqB;wBACrB,mBAAmB;wBACnB,OAAO;4BACL,GAAI,CAAC,MAAM,eAAe,GAAG,eAAe,CAAC,CAAC;4BAC9C,kGAAkG;4BAClG,wEAAwE;4BACxE,eAAe,kBAAkB,IAAI,kBAAkB,QAAQ,QAAQ,CAAC,MAAM,GAAG;4BACjF,eAAe,QAAQ,QAAQ,CAAC,MAAM;4BACtC,qBAAqB,kBAAkB,QAAQ,QAAQ,CAAC,MAAM;wBAChE;wBACA,WAAW,CAAA,cAAe,AAAC,CAAA,CAAC,MAAM,eAAe,GAAG,mBAAmB,EAAC,IAAK,qCAAe;gCAAC,GAAG,WAAW;gCAAE,WAAW,MAAM,YAAY,KAAK;4BAAS,GAAG,CAAC,MAAM,eAAe,GAAG,SAAS;kCAC5L;;;;;;IAQb,iFAAiF;IACjF,yFAAyF;IACzF,IAAI,MAAM,eAAe,EACvB,qBACE,iCAAC;QAAI,KAAK;QAAQ,WAAW,mBAAmB,oCAAc,MAAM;QAAS,OAAO;;YACjF;YACA;;;IAKP,OAAO;AACT","sources":["packages/@react-spectrum/s2/src/CardView.tsx"],"sourcesContent":["/*\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 {\n GridList as AriaGridList,\n Collection,\n ContextValue,\n GridLayout,\n GridListItem,\n GridListProps,\n Size,\n UNSTABLE_GridListLoadingSentinel,\n Virtualizer,\n WaterfallLayout\n} from 'react-aria-components';\nimport {CardContext, InternalCardViewContext} from './Card';\nimport {createContext, forwardRef, ReactElement, useMemo, useRef, useState} from 'react';\nimport {DOMRef, DOMRefValue, forwardRefType, Key, LoadingState} from '@react-types/shared';\nimport {focusRing, style} from '../style' with {type: 'macro'};\nimport {getAllowedOverrides, StylesPropWithHeight, UnsafeStyles} from './style-utils' with {type: 'macro'};\nimport {ImageCoordinator} from './ImageCoordinator';\nimport {useActionBarContainer} from './ActionBar';\nimport {useDOMRef} from '@react-spectrum/utils';\nimport {useEffectEvent, useLayoutEffect, useResizeObserver} from '@react-aria/utils';\nimport {useSpectrumContextProps} from './useSpectrumContextProps';\n\nexport interface CardViewProps<T> extends Omit<GridListProps<T>, 'layout' | 'keyboardNavigationBehavior' | 'selectionBehavior' | 'className' | 'style' | 'isLoading'>, UnsafeStyles {\n /**\n * The layout of the cards.\n * @default 'grid'\n */\n layout?: 'grid' | 'waterfall',\n /**\n * The size of the cards.\n * @default 'M'\n */\n size?: 'XS' | 'S' | 'M' | 'L' | 'XL',\n /**\n * The amount of space between the cards.\n * @default 'regular'\n */\n density?: 'compact' | 'regular' | 'spacious',\n /**\n * The visual style of the cards.\n * @default 'primary'\n */\n variant?: 'primary' | 'secondary' | 'tertiary' | 'quiet',\n /**\n * How selection should be displayed.\n * @default 'checkbox'\n */\n selectionStyle?: 'checkbox' | 'highlight',\n /** The loading state of the CardView. */\n loadingState?: LoadingState,\n /** Handler that is called when more items should be loaded, e.g. while scrolling near the bottom. */\n onLoadMore?: () => void,\n /** Spectrum-defined styles, returned by the `style()` macro. */\n styles?: StylesPropWithHeight,\n /** Provides the ActionBar to render when cards are selected in the CardView. */\n renderActionBar?: (selectedKeys: 'all' | Set<Key>) => ReactElement\n}\n\nconst layoutOptions = {\n XS: {\n compact: {\n minSpace: new Size(6, 6),\n minItemSize: new Size(100, 100),\n maxItemSize: new Size(140, 140)\n },\n regular: {\n minSpace: new Size(8, 8),\n minItemSize: new Size(100, 100),\n maxItemSize: new Size(140, 140)\n },\n spacious: {\n minSpace: new Size(12, 12),\n minItemSize: new Size(100, 100),\n maxItemSize: new Size(140, 140)\n }\n },\n S: {\n compact: {\n minSpace: new Size(8, 8),\n minItemSize: new Size(150, 150),\n maxItemSize: new Size(210, 210)\n },\n regular: {\n minSpace: new Size(12, 12),\n minItemSize: new Size(150, 150),\n maxItemSize: new Size(210, 210)\n },\n spacious: {\n minSpace: new Size(16, 16),\n minItemSize: new Size(150, 150),\n maxItemSize: new Size(210, 210)\n }\n },\n M: {\n compact: {\n minSpace: new Size(12, 12),\n minItemSize: new Size(200, 200),\n maxItemSize: new Size(280, 280)\n },\n regular: {\n minSpace: new Size(16, 16),\n minItemSize: new Size(200, 200),\n maxItemSize: new Size(280, 280)\n },\n spacious: {\n minSpace: new Size(20, 20),\n minItemSize: new Size(200, 200),\n maxItemSize: new Size(280, 280)\n }\n },\n L: {\n compact: {\n minSpace: new Size(16, 16),\n minItemSize: new Size(270, 270),\n maxItemSize: new Size(370, 370)\n },\n regular: {\n minSpace: new Size(20, 20),\n minItemSize: new Size(270, 270),\n maxItemSize: new Size(370, 370)\n },\n spacious: {\n minSpace: new Size(24, 24),\n minItemSize: new Size(270, 270),\n maxItemSize: new Size(370, 370)\n }\n },\n XL: {\n compact: {\n minSpace: new Size(20, 20),\n minItemSize: new Size(340, 340),\n maxItemSize: new Size(460, 460)\n },\n regular: {\n minSpace: new Size(24, 24),\n minItemSize: new Size(340, 340),\n maxItemSize: new Size(460, 460)\n },\n spacious: {\n minSpace: new Size(28, 28),\n minItemSize: new Size(340, 340),\n maxItemSize: new Size(460, 460)\n }\n }\n};\n\nconst SIZES = ['XS', 'S', 'M', 'L', 'XL'] as const;\n\nconst cardViewStyles = style({\n overflowY: {\n default: 'auto',\n isLoading: 'hidden'\n },\n display: {\n isEmpty: 'flex'\n },\n boxSizing: 'border-box',\n flexDirection: 'column',\n alignItems: 'center',\n justifyContent: 'center',\n ...focusRing(),\n outlineStyle: {\n default: 'none',\n isEmpty: {\n isFocusVisible: 'solid'\n }\n },\n outlineOffset: -2\n}, getAllowedOverrides({height: true}));\n\nconst wrapperStyles = style({\n position: 'relative',\n overflow: 'clip',\n size: 'fit'\n}, getAllowedOverrides({height: true}));\n\nexport const CardViewContext = createContext<ContextValue<Partial<CardViewProps<any>>, DOMRefValue<HTMLDivElement>>>(null);\n\nexport const CardView = /*#__PURE__*/ (forwardRef as forwardRefType)(function CardView<T extends object>(props: CardViewProps<T>, ref: DOMRef<HTMLDivElement>) {\n [props, ref] = useSpectrumContextProps(props, ref, CardViewContext);\n let {\n children,\n layout: layoutName = 'grid',\n size: sizeProp = 'M',\n density = 'regular',\n variant = 'primary',\n selectionStyle = 'checkbox',\n UNSAFE_className = '',\n UNSAFE_style,\n styles,\n onLoadMore,\n items,\n ...otherProps} = props;\n let domRef = useDOMRef(ref);\n let innerRef = useRef(null);\n let scrollRef = props.renderActionBar ? innerRef : domRef;\n\n // This calculates the maximum t-shirt size where at least two columns fit in the available width.\n let [maxSizeIndex, setMaxSizeIndex] = useState(SIZES.length - 1);\n let updateSize = useEffectEvent(() => {\n let w = scrollRef.current?.clientWidth ?? 0;\n let i = SIZES.length - 1;\n while (i > 0) {\n let opts = layoutOptions[SIZES[i]][density];\n if (w >= opts.minItemSize.width * 2 + opts.minSpace.width * 3) {\n break;\n }\n i--;\n }\n setMaxSizeIndex(i);\n });\n\n useResizeObserver({\n ref: scrollRef,\n box: 'border-box',\n onResize: updateSize\n });\n\n useLayoutEffect(() => {\n updateSize();\n }, [updateSize]);\n\n // The actual rendered t-shirt size is the minimum between the size prop and the maximum possible size.\n let size = SIZES[Math.min(maxSizeIndex, SIZES.indexOf(sizeProp))];\n let layout = layoutName === 'waterfall' ? WaterfallLayout : GridLayout;\n let options = layoutOptions[size][density];\n\n let ctx = useMemo(() => ({size, variant}), [size, variant]);\n\n let {selectedKeys, onSelectionChange, actionBar, actionBarHeight} = useActionBarContainer({...props, scrollRef});\n\n let renderer;\n let cardLoadingSentinel = (\n <UNSTABLE_GridListLoadingSentinel\n onLoadMore={onLoadMore} />\n );\n\n if (typeof children === 'function' && items) {\n renderer = (\n <>\n <Collection items={items} dependencies={props.dependencies}>\n {children}\n </Collection>\n {cardLoadingSentinel}\n </>\n );\n } else {\n renderer = (\n <>\n {children}\n {cardLoadingSentinel}\n </>\n );\n }\n\n let cardView = (\n <Virtualizer layout={layout} layoutOptions={options}>\n <InternalCardViewContext.Provider value={GridListItem}>\n <CardContext.Provider value={ctx}>\n <ImageCoordinator>\n <AriaGridList\n ref={scrollRef}\n {...otherProps}\n items={items}\n layout=\"grid\"\n selectionBehavior={selectionStyle === 'highlight' ? 'replace' : 'toggle'}\n selectedKeys={selectedKeys}\n defaultSelectedKeys={undefined}\n onSelectionChange={onSelectionChange}\n style={{\n ...(!props.renderActionBar ? UNSAFE_style : {}),\n // Add padding at the bottom when the action bar is visible so users can scroll to the last items.\n // Also add scroll padding so keyboard navigating preserves the padding.\n paddingBottom: actionBarHeight > 0 ? actionBarHeight + options.minSpace.height : 0,\n scrollPadding: options.minSpace.height,\n scrollPaddingBottom: actionBarHeight + options.minSpace.height\n }}\n className={renderProps => (!props.renderActionBar ? UNSAFE_className : '') + cardViewStyles({...renderProps, isLoading: props.loadingState === 'loading'}, !props.renderActionBar ? styles : undefined)}>\n {renderer}\n </AriaGridList>\n </ImageCoordinator>\n </CardContext.Provider>\n </InternalCardViewContext.Provider>\n </Virtualizer>\n );\n\n // Add extra wrapper if there is an action bar so we can position relative to it.\n // ActionBar cannot be inside the GridList due to ARIA and focus management requirements.\n if (props.renderActionBar) {\n return (\n <div ref={domRef} className={UNSAFE_className + wrapperStyles(null, styles)} style={UNSAFE_style}>\n {cardView}\n {actionBar}\n </div>\n );\n }\n\n return cardView;\n});\n"],"names":[],"version":3,"file":"CardView.cjs.map"}