UNPKG

@react-spectrum/s2

Version:
1 lines 10.4 kB
{"mappings":"ACiKuB;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAsBD;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA","sources":["bcfa03266ec99ed3","packages/@react-spectrum/s2/src/CardView.tsx"],"sourcesContent":["@import \"b9a56ec855293cb6\";\n@import \"879fd470e85635cf\";\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 {\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.css.map"}