Skip to content

Commit

Permalink
feat(components): add GridList
Browse files Browse the repository at this point in the history
  • Loading branch information
Niznikr committed Nov 7, 2024
1 parent 42f63c5 commit 4dd4889
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 1 deletion.
63 changes: 63 additions & 0 deletions packages/components/src/GridList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import type { forwardRefType } from '@react-types/shared';
import type { ForwardedRef } from 'react';
import type { GridListItemProps, GridListProps } from 'react-aria-components';

import { cva } from 'class-variance-authority';
import { forwardRef } from 'react';
import {
GridList as AriaGridList,
GridListItem as AriaGridListItem,
composeRenderProps,
} from 'react-aria-components';

import styles from './styles/GridList.module.css';

const list = cva(styles.list);
const item = cva(styles.item);

const _GridList = <T extends object>(
props: GridListProps<T>,
ref: ForwardedRef<HTMLDivElement>,
) => {
return (
<AriaGridList
{...props}
ref={ref}
className={composeRenderProps(props.className, (className, renderProps) =>
list({ ...renderProps, className }),
)}
/>
);
};

/**
* A grid list displays a list of interactive items, with support for keyboard navigation, single or multiple selection, and row actions.
*
* https://react-spectrum.adobe.com/react-aria/GridList.html
*/
const GridList = (forwardRef as forwardRefType)(_GridList);

const _GridListItem = <T extends object>(props: GridListItemProps<T>, ref: ForwardedRef<T>) => {
const textValue =
props.textValue || (typeof props.children === 'string' ? props.children : undefined);
return (
<AriaGridListItem
textValue={textValue}
{...props}
ref={ref}
className={composeRenderProps(props.className, (className, renderProps) =>
item({ ...renderProps, className }),
)}
/>
);
};

/**
* A GridListItem represents an individual item in a GridList.
*
* https://react-spectrum.adobe.com/react-aria/GridList.html
*/
const GridListItem = (forwardRef as forwardRefType)(_GridListItem);

export { GridList, GridListItem };
export type { GridListProps, GridListItemProps };
2 changes: 2 additions & 0 deletions packages/components/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export type { FieldErrorProps } from './FieldError';
export type { FieldGroupProps } from './FieldGroup';
export type { FileTriggerProps } from './FileTrigger';
export type { FormProps } from './Form';
export type { GridListProps, GridListItemProps } from './GridList';
export type { GroupProps } from './Group';
export type { HeadingProps } from './Heading';
export type { InputProps } from './Input';
Expand Down Expand Up @@ -102,6 +103,7 @@ export { FieldError } from './FieldError';
export { FieldGroup } from './FieldGroup';
export { FileTrigger } from './FileTrigger';
export { Form } from './Form';
export { GridList, GridListItem } from './GridList';
export { Group } from './Group';
export { Header } from './Header';
export { Heading } from './Heading';
Expand Down
17 changes: 17 additions & 0 deletions packages/components/src/styles/GridList.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.list {
display: flex;
flex-direction: column;
}

.item {
composes: interactive from './base.module.css';
display: flex;
align-items: center;
box-shadow: inset 0 -1px 0 0 var(--lp-color-border-ui-secondary);
outline: none;
padding-block: var(--lp-spacing-300);

&[data-hovered] {
background-color: var(--lp-color-bg-ui-secondary);
}
}
32 changes: 32 additions & 0 deletions packages/components/stories/GridList.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type { Meta, StoryObj } from '@storybook/react';

import { GridList, GridListItem } from '../src';

const meta: Meta<typeof GridList> = {
component: GridList,
// @ts-ignore
subcomponents: { GridListItem },
title: 'Components/Collections/GridList',
parameters: {
status: {
type: import.meta.env.STORYBOOK_PACKAGE_STATUS__COMPONENTS,
},
},
};

export default meta;

type Story = StoryObj<typeof GridList>;

export const Example: Story = {
args: {
children: (
<>
<GridListItem>Item one</GridListItem>
<GridListItem>Item two</GridListItem>
<GridListItem>Item three</GridListItem>
</>
),
selectionMode: 'single',
},
};
1 change: 0 additions & 1 deletion packages/components/stories/ListBox.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable react-hooks/rules-of-hooks */
import type { Meta, StoryObj } from '@storybook/react';
import type { Selection as AriaSelection } from 'react-aria-components';

Expand Down

0 comments on commit 4dd4889

Please sign in to comment.