Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/@react-spectrum/cards/src/CardView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ import {SpectrumCardViewProps} from '@react-types/cards';
import {useCollator, useLocale, useMessageFormatter} from '@react-aria/i18n';
import {useGrid, useGridCell, useGridRow} from '@react-aria/grid';
import {useListState} from '@react-stately/list';
import {useProvider} from '@react-spectrum/provider';
import {Virtualizer, VirtualizerItem} from '@react-aria/virtualizer';

function CardView<T extends object>(props: SpectrumCardViewProps<T>, ref: DOMRef<HTMLDivElement>) {
let {scale} = useProvider();
let {styleProps} = useStyleProps(props);
let domRef = useDOMRef(ref);
let {
Expand All @@ -40,6 +42,15 @@ function CardView<T extends object>(props: SpectrumCardViewProps<T>, ref: DOMRef
let collator = useCollator({usage: 'search', sensitivity: 'base'});
let isLoading = loadingState === 'loading' || loadingState === 'loadingMore';
let cardViewLayout = useMemo(() => typeof layout === 'function' ? new layout({collator}) : layout, [layout, collator]);
let layoutType = cardViewLayout.layoutType;

if (typeof layout === 'function') {
if (layoutType === 'grid') {
cardViewLayout.itemPadding = scale === 'large' ? 116 : 95;
} else if (layoutType === 'gallery') {
cardViewLayout.itemPadding = scale === 'large' ? 143 : 114;
}
}
Comment on lines +47 to +53
Copy link
Copy Markdown
Member Author

@LFDanLu LFDanLu Sep 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If user passes a layout constructor to the CardView we have set the itemPadding for the user depending on the scale since the layout doesn't have a concept of scale. Perhaps the layout should have a scale option?


let formatMessage = useMessageFormatter(intlMessages);
let {direction} = useLocale();
Expand Down
2 changes: 1 addition & 1 deletion packages/@react-spectrum/cards/src/GalleryLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export class GalleryLayout<T> extends BaseLayout<T> {
protected idealRowHeight: number;
protected margin: number;
protected itemSpacing: Size;
protected itemPadding: number;
itemPadding: number;
protected minItemSize: Size;
protected threshold: number;

Expand Down
2 changes: 1 addition & 1 deletion packages/@react-spectrum/cards/src/GridLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export class GridLayout<T> extends BaseLayout<T> {
protected margin: number;
protected minSpace: Size;
protected maxColumns: number;
protected itemPadding: number;
itemPadding: number;
protected itemSize: Size;
protected numColumns: number;
protected numRows: number;
Expand Down
2 changes: 1 addition & 1 deletion packages/@react-spectrum/cards/src/WaterfallLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class WaterfallLayout<T> extends BaseLayout<T> implements KeyboardDelegat
protected margin: number;
protected minSpace: Size;
protected maxColumns: number;
protected itemPadding: number;
itemPadding: number;
protected numColumns: number;
protected itemWidth: number;
protected horizontalSpacing: number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import {AsyncLoadingCardView, ControlledCardView, CustomLayout, DynamicCardView, items, NoItemCardView, renderEmptyState, StaticCardView} from './GridCardView.stories';
import {GalleryLayout} from '../';
import React from 'react';
import {Size} from '@react-stately/virtualizer';
import {useCollator} from '@react-aria/i18n';
import {useMemo} from 'react';
Expand Down Expand Up @@ -57,8 +58,11 @@ let itemsNoThinImages = [
{width: 1516, height: 1009, src: 'https://i.imgur.com/1nScMIH.jpg', id: 21, title: 'Bob 5'}
];

const StoryFn = ({storyFn}) => storyFn();

export default {
title: 'CardView/Gallery layout'
title: 'CardView/Gallery layout',
decorators: [storyFn => <StoryFn storyFn={storyFn} />]
};

export const DefaultGalleryStatic = () => StaticCardView({layout: GalleryLayout, items});
Expand Down
56 changes: 49 additions & 7 deletions packages/@react-spectrum/cards/stories/GridCardView.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {Size} from '@react-stately/virtualizer';
import {TextField} from '@react-spectrum/textfield';
import {useAsyncList} from '@react-stately/data';
import {useCollator} from '@react-aria/i18n';
import {useProvider} from '@react-spectrum/provider';

export let items = [
{width: 1001, height: 381, src: 'https://i.imgur.com/Z7AzH2c.jpg', title: 'Bob 1'},
Expand Down Expand Up @@ -63,9 +64,12 @@ export function renderEmptyState() {
);
}

const StoryFn = ({storyFn}) => storyFn();

export default {
title: 'CardView/Grid layout',
excludeStories: ['items', 'renderEmptyState', 'DynamicCardView', 'NoItemCardView', 'StaticCardView', 'ControlledCardView', 'AsyncLoadingCardView', 'CustomLayout']
excludeStories: ['items', 'renderEmptyState', 'DynamicCardView', 'NoItemCardView', 'StaticCardView', 'ControlledCardView', 'AsyncLoadingCardView', 'CustomLayout'],
decorators: [storyFn => <StoryFn storyFn={storyFn} />]
};

let onSelectionChange = action('onSelectionChange');
Expand Down Expand Up @@ -118,8 +122,14 @@ export const CustomLayoutOptions = () => CustomLayout({items}, {maxColumns: 2, m
CustomLayoutOptions.storyName = 'Custom layout options';

export function DynamicCardView(props) {
let {scale} = useProvider();
let collator = useCollator({usage: 'search', sensitivity: 'base'});
let gridLayout = useMemo(() => new GridLayout({collator}), [collator]);
let gridLayout = useMemo(() =>
new GridLayout({
itemPadding: scale === 'large' ? 116 : 95,
collator
})
, [collator, scale]);
let {
layout = gridLayout,
selectionMode = 'multiple',
Expand Down Expand Up @@ -164,8 +174,14 @@ export function DynamicCardView(props) {
}

export function ControlledCardView(props) {
let {scale} = useProvider();
let collator = useCollator({usage: 'search', sensitivity: 'base'});
let gridLayout = useMemo(() => new GridLayout({collator}), [collator]);
let gridLayout = useMemo(() =>
new GridLayout({
itemPadding: scale === 'large' ? 116 : 95,
collator
})
, [collator, scale]);
let {
layout = gridLayout,
selectionMode = 'multiple',
Expand Down Expand Up @@ -212,7 +228,14 @@ export function ControlledCardView(props) {
}

export function NoItemCardView(props) {
let gridLayout = useMemo(() => new GridLayout({}), []);
let {scale} = useProvider();
let collator = useCollator({usage: 'search', sensitivity: 'base'});
let gridLayout = useMemo(() =>
new GridLayout({
itemPadding: scale === 'large' ? 116 : 95,
collator
})
, [collator, scale]);
let {
layout = gridLayout
} = props;
Expand Down Expand Up @@ -243,8 +266,14 @@ export function NoItemCardView(props) {
}

export function StaticCardView(props) {
let {scale} = useProvider();
let collator = useCollator({usage: 'search', sensitivity: 'base'});
let gridLayout = useMemo(() => new GridLayout({collator}), [collator]);
let gridLayout = useMemo(() =>
new GridLayout({
itemPadding: scale === 'large' ? 116 : 95,
collator
})
, [collator, scale]);
let {
layout = gridLayout
} = props;
Expand Down Expand Up @@ -317,8 +346,14 @@ export function AsyncLoadingCardView(props) {
url: string
}

let {scale} = useProvider();
let collator = useCollator({usage: 'search', sensitivity: 'base'});
let gridLayout = useMemo(() => new GridLayout({collator}), [collator]);
let gridLayout = useMemo(() =>
new GridLayout({
itemPadding: scale === 'large' ? 116 : 95,
collator
})
, [collator, scale]);
let {
layout = gridLayout
} = props;
Expand Down Expand Up @@ -365,8 +400,15 @@ export function AsyncLoadingCardView(props) {
}

export function CustomLayout(props, layoutOptions) {
let {scale} = useProvider();
let collator = useCollator({usage: 'search', sensitivity: 'base'});
let gridLayout = useMemo(() => new GridLayout({collator, ...layoutOptions}), [collator, layoutOptions]);
let gridLayout = useMemo(() =>
new GridLayout({
itemPadding: scale === 'large' ? 116 : 95,
collator,
...layoutOptions
})
, [collator, scale, layoutOptions]);
let {
layout = gridLayout,
selectionMode = 'multiple',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/

import {AsyncLoadingCardView, ControlledCardView, CustomLayout, DynamicCardView, items, NoItemCardView, renderEmptyState, StaticCardView} from './GridCardView.stories';
import React from 'react';
import {Size} from '@react-stately/virtualizer';
import {useCollator} from '@react-aria/i18n';
import {useMemo} from 'react';
Expand Down Expand Up @@ -41,8 +42,11 @@ let itemsNoSize = [
{src: 'https://i.imgur.com/zzwWogn.jpg', title: 'Bob 8'}
];

const StoryFn = ({storyFn}) => storyFn();

export default {
title: 'CardView/Waterfall layout'
title: 'CardView/Waterfall layout',
decorators: [storyFn => <StoryFn storyFn={storyFn} />]
};

export const DefaultWaterfallStatic = () => StaticCardView({layout: WaterfallLayout, items});
Expand Down
4 changes: 3 additions & 1 deletion packages/@react-types/cards/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ interface CardViewLayout<T> extends Layout<Node<T>>, KeyboardDelegate {
collection: Collection<Node<T>>,
disabledKeys: any,
isLoading: boolean,
direction: Direction
direction: Direction,
layoutType: string,
itemPadding: number
}

export interface CardViewLayoutConstructor<T> {
Expand Down