-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9bc2042
commit 380c9f3
Showing
6 changed files
with
256 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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,12 @@ | ||
.flexItem { | ||
min-width: var(--min-width); | ||
max-width: var(--max-width); | ||
} | ||
|
||
.none { | ||
flex: none; | ||
} | ||
|
||
.longhand { | ||
flex: var(--flex-grow) var(--flex-shrink) var(--flex-basis); | ||
} |
This file contains 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,84 @@ | ||
import { render, screen } from '@testing-library/react'; | ||
import { FlexItem } from './FlexItem'; | ||
|
||
describe('<FlexItem>', () => { | ||
it('longhand class is only given if an individual flex property is specified.', () => { | ||
render( | ||
<FlexItem flex={{ grow: 1 }} data-testid="flex-item"> | ||
Test | ||
</FlexItem>, | ||
); | ||
const div = screen.getByTestId('flex-item'); | ||
|
||
expect(div).toHaveClass(/longhand/i); | ||
}); | ||
|
||
it('Default values for individual properties not specified', () => { | ||
render( | ||
<div> | ||
<FlexItem | ||
flex={{ | ||
grow: 0, | ||
}} | ||
data-testid="flex-item-1" | ||
> | ||
Test | ||
</FlexItem> | ||
<FlexItem | ||
flex={{ | ||
shrink: 1, | ||
}} | ||
data-testid="flex-item-2" | ||
> | ||
Test | ||
</FlexItem> | ||
</div>, | ||
); | ||
const div1 = screen.getByTestId('flex-item-1'); | ||
const div2 = screen.getByTestId('flex-item-2'); | ||
|
||
expect(div1).toHaveStyle('--flex-shrink: 1'); | ||
expect(div1).toHaveStyle('--flex-basis: auto'); | ||
expect(div2).toHaveStyle('--flex-grow: 0'); | ||
}); | ||
|
||
it('If no individual flex peroperty is specified, the longhand class is not specified', () => { | ||
render(<FlexItem data-testid="flex-item">Test</FlexItem>); | ||
const div = screen.getByTestId('flex-item'); | ||
|
||
expect(div).not.toHaveClass(/longhand/i); | ||
}); | ||
|
||
it('If none is specified, the none class is given', () => { | ||
render( | ||
<FlexItem flex="none" data-testid="flex-item"> | ||
Test | ||
</FlexItem>, | ||
); | ||
const div = screen.getByTestId('flex-item'); | ||
|
||
expect(div).toHaveClass(/none/i); | ||
}); | ||
|
||
it('Receive max-width', () => { | ||
render( | ||
<FlexItem maxWidth="100px" data-testid="flex-item"> | ||
Test | ||
</FlexItem>, | ||
); | ||
const div = screen.getByTestId('flex-item'); | ||
|
||
expect(div).toHaveStyle('--max-width: 100px'); | ||
}); | ||
|
||
it('Receive min-width', () => { | ||
render( | ||
<FlexItem minWidth="100px" data-testid="flex-item"> | ||
Test | ||
</FlexItem>, | ||
); | ||
const div = screen.getByTestId('flex-item'); | ||
|
||
expect(div).toHaveStyle('--min-width: 100px'); | ||
}); | ||
}); |
This file contains 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,65 @@ | ||
'use client'; | ||
|
||
import { clsx } from 'clsx'; | ||
import { CSSProperties, forwardRef, type PropsWithChildren, type HTMLAttributes } from 'react'; | ||
import styles from './FlexItem.module.css'; | ||
import { CSSWitdh, CSSMaxWidth, CSSMinWidth } from '../../utils/types'; | ||
|
||
type FlexProperty = { | ||
grow?: number; | ||
shrink?: number; | ||
basis?: CSSWitdh; | ||
}; | ||
|
||
type Props = { | ||
/** | ||
* flexの値を指定。 growなどを指定したい場合はオブジェクトで指定 | ||
* @defaultValue none | ||
*/ | ||
flex?: 'none' | FlexProperty; | ||
/** | ||
* 最小幅 | ||
* @defaultValue auto | ||
*/ | ||
minWidth?: CSSMinWidth; | ||
/** | ||
* 最大幅 | ||
* @defaultValue none | ||
*/ | ||
maxWidth?: CSSMaxWidth; | ||
} & HTMLAttributes<HTMLDivElement>; | ||
|
||
/** | ||
* FlexやStackの子として配置し、レイアウトを調整 | ||
*/ | ||
export const FlexItem = forwardRef<HTMLDivElement, PropsWithChildren<Props>>( | ||
({ children, flex = 'none', minWidth = 'auto', maxWidth = 'none', ...rest }, ref) => { | ||
const flexObj: { [key: string]: string } = | ||
typeof flex === 'object' | ||
? { | ||
'--flex-grow': flex.grow != null ? flex.grow.toString() : '0', | ||
'--flex-shrink': flex.shrink != null ? flex.shrink.toString() : '1', | ||
'--flex-basis': flex.basis ?? 'auto', | ||
} | ||
: {}; | ||
|
||
return ( | ||
<div | ||
className={clsx(styles.flexItem, flex === 'none' && styles.none, typeof flex === 'object' && styles.longhand)} | ||
ref={ref} | ||
style={ | ||
{ | ||
'--min-width': minWidth, | ||
'--max-width': maxWidth, | ||
...flexObj, | ||
} as CSSProperties | ||
} | ||
{...rest} | ||
> | ||
{children} | ||
</div> | ||
); | ||
}, | ||
); | ||
|
||
FlexItem.displayName = 'FlexItem'; |
This file contains 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 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 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