-
Notifications
You must be signed in to change notification settings - Fork 0
CC-41: <Select/> is Missing size Prop #22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import { | ||
| CSSProperties, | ||
| Ref, | ||
| useEffect, | ||
| useRef, | ||
| useState, | ||
| } from 'react'; | ||
|
|
||
| export type MasonryConfig = { | ||
| minColumns?: number; | ||
| maxColumns?: number; | ||
| minColumnWidth?: number; | ||
| gap?: number; | ||
| columnGap?: number; | ||
| rowGap?: number; | ||
| }; | ||
|
|
||
| type UseMasonryResult = { | ||
| ref: Ref<HTMLDivElement>; | ||
| columnCount: number; | ||
| wrapperStyle: CSSProperties; | ||
| }; | ||
|
|
||
| export const useMasonry = (config: MasonryConfig): UseMasonryResult => { | ||
| const { | ||
| minColumns = 1, | ||
| maxColumns = 4, | ||
| minColumnWidth = 200, | ||
| gap = 16, | ||
| columnGap, | ||
| rowGap, | ||
| } = config; | ||
|
|
||
| const resolvedColumnGap = columnGap ?? gap; | ||
| const resolvedRowGap = rowGap ?? gap; | ||
|
|
||
| const clampedMinColumns = Math.min(minColumns, maxColumns); | ||
| const clampedMaxColumns = Math.max(minColumns, maxColumns); | ||
|
|
||
| const ref = useRef<HTMLDivElement>(null); | ||
| const [columnCount, setColumnCount] = useState(minColumns); | ||
|
|
||
| useEffect(() => { | ||
| const element = ref.current; | ||
| if (!element) { | ||
| return; | ||
| } | ||
|
|
||
| const updateColumns = (containerWidth: number): void => { | ||
| const effectiveGap = parseFloat(getComputedStyle(element).getPropertyValue('--masonry-column-gap')) || resolvedColumnGap; | ||
| const rawColumns = Math.floor((containerWidth + effectiveGap) / (minColumnWidth + effectiveGap)); | ||
| const clampedColumns = rawColumns > 0 ? rawColumns : clampedMinColumns; | ||
| setColumnCount(Math.min(clampedMaxColumns, Math.max(clampedMinColumns, clampedColumns))); | ||
| }; | ||
|
|
||
| const resizeObserver = new ResizeObserver(([entry]) => { | ||
| const containerWidth = entry.contentBoxSize?.[0]?.inlineSize ?? entry.contentRect.width; | ||
| updateColumns(containerWidth); | ||
| }); | ||
|
|
||
| resizeObserver.observe(element); | ||
| updateColumns(element.offsetWidth); | ||
| return () => resizeObserver.disconnect(); | ||
| }, [clampedMinColumns, clampedMaxColumns, minColumnWidth, resolvedColumnGap]); | ||
|
|
||
| return { | ||
| ref, | ||
| columnCount, | ||
| wrapperStyle: { | ||
| ['--masonry-column-gap' as string]: `${resolvedColumnGap}px`, | ||
| ['--masonry-row-gap' as string]: `${resolvedRowGap}px`, | ||
| }, | ||
| }; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| .masonry-grid { | ||
| display: grid; | ||
| gap: var(--masonry-column-gap); | ||
| align-items: start; | ||
|
ianpaschal marked this conversation as resolved.
|
||
|
|
||
| &-column { | ||
| display: flex; | ||
| flex-direction: column; | ||
| gap: var(--masonry-row-gap); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export declare const masonryGrid: string; | ||
| export declare const masonryGridColumn: string; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import type { Meta, StoryObj } from '@storybook/react'; | ||
|
|
||
| import { MasonryGrid } from './MasonryGrid'; | ||
|
|
||
| const SAMPLE_ITEMS = [ | ||
| { height: 120, color: '#c7f2d3' }, | ||
| { height: 200, color: '#fde8c8' }, | ||
| { height: 80, color: '#d4e4fb' }, | ||
| { height: 160, color: '#f9d4d4' }, | ||
| { height: 100, color: '#e8d5fb' }, | ||
| { height: 220, color: '#d4f5fb' }, | ||
| { height: 140, color: '#fbf3d4' }, | ||
| { height: 90, color: '#fbd4ee' }, | ||
| { height: 180, color: '#d4fbdf' }, | ||
| { height: 110, color: '#fbddd4' }, | ||
| { height: 130, color: '#d4d9fb' }, | ||
| { height: 75, color: '#f2fbd4' }, | ||
| ]; | ||
|
|
||
| const meta = { | ||
| title: 'Masonry Grid', | ||
| component: MasonryGrid, | ||
| parameters: { | ||
| layout: 'padded', | ||
| }, | ||
| tags: ['autodocs'], | ||
| argTypes: { | ||
| minColumns: { control: { type: 'range', min: 1, max: 6, step: 1 } }, | ||
| maxColumns: { control: { type: 'range', min: 1, max: 8, step: 1 } }, | ||
| minColumnWidth: { control: { type: 'range', min: 80, max: 400, step: 10 } }, | ||
| gap: { control: { type: 'range', min: 0, max: 48, step: 2 } }, | ||
| columnGap: { control: { type: 'range', min: 0, max: 48, step: 2 } }, | ||
| rowGap: { control: { type: 'range', min: 0, max: 48, step: 2 } }, | ||
| }, | ||
| } satisfies Meta<typeof MasonryGrid>; | ||
|
|
||
| export default meta; | ||
| type Story = StoryObj<typeof meta>; | ||
|
|
||
| const SampleChildren = SAMPLE_ITEMS.map((item, index) => ( | ||
| <div | ||
| key={index} | ||
| style={{ | ||
| height: item.height, | ||
| background: item.color, | ||
| borderRadius: 8, | ||
| display: 'flex', | ||
| alignItems: 'center', | ||
| justifyContent: 'center', | ||
| fontWeight: 600, | ||
| color: '#555', | ||
| fontSize: 13, | ||
| }} | ||
| > | ||
| #{index + 1} · {item.height}px | ||
| </div> | ||
| )); | ||
|
|
||
| export const Default: Story = { | ||
| args: { | ||
| minColumns: 1, | ||
| maxColumns: 4, | ||
| minColumnWidth: 150, | ||
| columnGap: 12, | ||
| rowGap: 12, | ||
| }, | ||
| render: (args) => <MasonryGrid {...args}>{SampleChildren}</MasonryGrid>, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import React, { Children } from 'react'; | ||
|
|
||
| import { MasonryConfig, useMasonry } from './MasonryGrid.hooks'; | ||
|
|
||
| import styles from './MasonryGrid.module.scss'; | ||
|
|
||
| /** | ||
| * MasonryGrid | ||
| * | ||
| * Props: | ||
| * minColumns {number} Minimum number of columns (default: 1) | ||
| * maxColumns {number} Maximum number of columns (default: 4) | ||
| * minColumnWidth {number} Minimum column width in px used to compute column count (default: 200) | ||
| * gap {number} Shorthand for both columnGap and rowGap (default: 16) | ||
| * columnGap {number} Horizontal gap between columns in px (overrides gap) | ||
| * rowGap {number} Vertical gap between items in px (overrides gap) | ||
| * | ||
| * children {node[]} Items to lay out | ||
| * className {string} Class name applied to the wrapper div | ||
| * style {object} Inline styles applied to the wrapper div | ||
| * | ||
| * CSS vars (set on the wrapper, overridable externally): | ||
| * --masonry-column-gap | ||
| * --masonry-row-gap | ||
| */ | ||
| export interface MasonryGridProps extends MasonryConfig { | ||
| children?: React.ReactNode; | ||
| className?: string; | ||
| style?: React.CSSProperties; | ||
| } | ||
|
|
||
| export const MasonryGrid = ({ | ||
| children, | ||
| className, | ||
| style, | ||
| ...props | ||
| }: MasonryGridProps) => { | ||
| const { ref, columnCount, wrapperStyle } = useMasonry(props); | ||
|
|
||
| // Distribute children into columns top-to-bottom, left-to-right: | ||
| const columns = Array.from({ length: columnCount }, () => [] as React.ReactNode[]); | ||
| Children.forEach(children, (child, index) => { | ||
| columns[index % columnCount].push(child); | ||
| }); | ||
|
ianpaschal marked this conversation as resolved.
|
||
|
|
||
| return ( | ||
| <div ref={ref} className={className} style={{ ...wrapperStyle, ...style }}> | ||
| <div className={styles.masonryGrid} style={{ gridTemplateColumns: `repeat(${columnCount}, 1fr)` }}> | ||
| {columns.map((column, columnIndex) => ( | ||
| <div key={columnIndex} className={styles.masonryGridColumn}> | ||
| {column.map((child, rowIndex) => ( | ||
| <div key={rowIndex}> | ||
| {child} | ||
| </div> | ||
| ))} | ||
|
ianpaschal marked this conversation as resolved.
|
||
| </div> | ||
| ))} | ||
| </div> | ||
| </div> | ||
| ); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from './MasonryGrid'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Initialize state with clamped value for consistency.
The initial state uses
minColumnsdirectly, but ifminColumns > maxColumns, the state will briefly hold an invalid value until the effect runs and corrects it. Initialize withclampedMinColumnsto maintain consistency from the first render.✨ Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents