-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Components: Add ItemGroup and Item #30097
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f73962a
Add ItemGroup and Item
sarayourfriend 4f942ce
Update UI design for item-group
9d5da24
Remove collabsible story
sarayourfriend f060506
Improve computed paddingY calculations for ItemGroup Item
e57fede
Remove g2
sarayourfriend 0bdaf89
Fix stories
diegohaz c2ef3ec
Rename DropdownMenu to Dropdown
diegohaz 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 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 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { createContext, useContext } from '@wordpress/element'; | ||
|
||
export const ItemGroupContext = createContext( { size: 'medium' } as { | ||
spacedAround: boolean; | ||
size: 'small' | 'medium' | 'large'; | ||
} ); | ||
|
||
export const useItemGroupContext = () => useContext( ItemGroupContext ); |
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,2 @@ | ||
export { default as Item } from './item'; | ||
export { default as ItemGroup } from './item-group'; |
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,35 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import type { PolymorphicComponentProps } from '../context'; | ||
// eslint-disable-next-line no-duplicate-imports | ||
import { contextConnect } from '../context'; | ||
import { useItemGroup } from './use-item-group'; | ||
// eslint-disable-next-line no-duplicate-imports | ||
import type { Props } from './use-item-group'; | ||
import { ItemGroupContext, useItemGroupContext } from './context'; | ||
import { View } from '../../view'; | ||
|
||
function ItemGroup( props: PolymorphicComponentProps< Props, 'div' > ) { | ||
const { bordered, separated, size: sizeProp, ...otherProps } = useItemGroup( | ||
props | ||
); | ||
|
||
const { size: contextSize } = useItemGroupContext(); | ||
|
||
const spacedAround = ! bordered && ! separated; | ||
const size = sizeProp || contextSize; | ||
|
||
const contextValue = { | ||
spacedAround, | ||
size, | ||
}; | ||
|
||
return ( | ||
<ItemGroupContext.Provider value={ contextValue }> | ||
<View { ...otherProps } /> | ||
</ItemGroupContext.Provider> | ||
); | ||
} | ||
|
||
export default contextConnect( ItemGroup, 'ItemGroup' ); |
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,11 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { createComponent } from '../utils'; | ||
import { useItem } from './use-item'; | ||
|
||
export default createComponent( { | ||
useHook: useItem, | ||
as: 'div', | ||
name: 'Item', | ||
} ); |
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,53 @@ | ||
/* eslint-disable no-alert */ | ||
/* globals alert */ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { ItemGroup, Item } from '..'; | ||
import { Popover } from '../../popover'; | ||
import Button from '../../../button'; | ||
|
||
export default { | ||
component: ItemGroup, | ||
title: 'Components (Experimental)/ItemGroup', | ||
}; | ||
|
||
export const _default = () => ( | ||
<ItemGroup css={ { width: '350px' } } bordered> | ||
<Item action onClick={ () => alert( 'WordPress.org' ) }> | ||
Code is Poetry — Click me! | ||
</Item> | ||
<Item action onClick={ () => alert( 'WordPress.org' ) }> | ||
Code is Poetry — Click me! | ||
</Item> | ||
<Item action onClick={ () => alert( 'WordPress.org' ) }> | ||
Code is Poetry — Click me! | ||
</Item> | ||
<Item action onClick={ () => alert( 'WordPress.org' ) }> | ||
Code is Poetry — Click me! | ||
</Item> | ||
</ItemGroup> | ||
); | ||
|
||
export const dropdown = () => ( | ||
<Popover | ||
css={ { width: '350px' } } | ||
trigger={ <Button>Open Popover</Button> } | ||
> | ||
<ItemGroup css={ { padding: 4 } }> | ||
<Item action onClick={ () => alert( 'WordPress.org' ) }> | ||
Code is Poetry — Click me! | ||
</Item> | ||
<Item action onClick={ () => alert( 'WordPress.org' ) }> | ||
Code is Poetry — Click me! | ||
</Item> | ||
<Item action onClick={ () => alert( 'WordPress.org' ) }> | ||
Code is Poetry — Click me! | ||
</Item> | ||
<Item action onClick={ () => alert( 'WordPress.org' ) }> | ||
Code is Poetry — Click me! | ||
</Item> | ||
</ItemGroup> | ||
</Popover> | ||
); | ||
/* eslint-enable no-alert */ |
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,93 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { css } from 'emotion'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { CONFIG } from '../../utils'; | ||
import COLORS from '../../utils/colors-values'; | ||
|
||
export const unstyledButton = css` | ||
appearance: none; | ||
border: 1px solid transparent; | ||
cursor: pointer; | ||
background: none; | ||
text-align: left; | ||
|
||
&:hover { | ||
color: ${ COLORS.admin.theme }; | ||
} | ||
|
||
&:focus { | ||
background-color: transparent; | ||
color: ${ COLORS.admin.theme }; | ||
border-color: ${ COLORS.admin.theme }; | ||
outline: 3px solid transparent; | ||
} | ||
`; | ||
|
||
export const item = css` | ||
width: 100%; | ||
display: block; | ||
`; | ||
|
||
export const bordered = css` | ||
border: 1px solid ${ CONFIG.surfaceBorderColor }; | ||
`; | ||
|
||
export const separated = css` | ||
> *:not( marquee ) { | ||
border-bottom: 1px solid ${ CONFIG.surfaceBorderColor }; | ||
} | ||
|
||
> *:last-child:not( :focus ) { | ||
border-bottom-color: transparent; | ||
} | ||
`; | ||
|
||
const borderRadius = CONFIG.controlBorderRadius; | ||
|
||
export const spacedAround = css` | ||
border-radius: ${ borderRadius }; | ||
`; | ||
|
||
export const rounded = css` | ||
border-radius: ${ borderRadius }; | ||
|
||
> *:first-child { | ||
border-top-left-radius: ${ borderRadius }; | ||
border-top-right-radius: ${ borderRadius }; | ||
} | ||
|
||
> *:last-child { | ||
border-bottom-left-radius: ${ borderRadius }; | ||
border-bottom-right-radius: ${ borderRadius }; | ||
} | ||
`; | ||
|
||
const baseFontHeight = `calc(${ CONFIG.fontSize } * ${ CONFIG.fontLineHeightBase })`; | ||
|
||
/* | ||
* Math: | ||
* - Use the desired height as the base value | ||
* - Subtract the computed height of (default) text | ||
* - Subtract the effects of border | ||
* - Divide the calculated number by 2, in order to get an individual top/bottom padding | ||
*/ | ||
const paddingY = `calc((${ CONFIG.controlHeight } - ${ baseFontHeight } - 2px) / 2)`; | ||
const paddingYSmall = `calc((${ CONFIG.controlHeightSmall } - ${ baseFontHeight } - 2px) / 2)`; | ||
const paddingYLarge = `calc((${ CONFIG.controlHeightLarge } - ${ baseFontHeight } - 2px) / 2)`; | ||
|
||
export const itemSizes = { | ||
small: css` | ||
padding: ${ paddingYSmall }, ${ CONFIG.controlPaddingXSmall }; | ||
`, | ||
medium: css` | ||
padding: ${ paddingY }, ${ CONFIG.controlPaddingX }; | ||
`, | ||
large: css` | ||
padding: ${ paddingYLarge }, ${ CONFIG.controlPaddingXLarge }; | ||
`, | ||
}; |
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,51 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { cx } from 'emotion'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { useContextSystem } from '../context'; | ||
// eslint-disable-next-line no-duplicate-imports | ||
import type { PolymorphicComponentProps } from '../context'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import * as styles from './styles'; | ||
|
||
export interface Props { | ||
bordered?: boolean; | ||
rounded?: boolean; | ||
separated?: boolean; | ||
size?: 'large' | 'medium' | 'small'; | ||
} | ||
|
||
export function useItemGroup( | ||
props: PolymorphicComponentProps< Props, 'div' > | ||
) { | ||
const { | ||
className, | ||
bordered = false, | ||
rounded = true, | ||
separated = false, | ||
role = 'list', | ||
...otherProps | ||
} = useContextSystem( props, 'ItemGroup' ); | ||
|
||
const classes = cx( | ||
bordered && styles.bordered, | ||
( bordered || separated ) && styles.separated, | ||
rounded && styles.rounded, | ||
className | ||
); | ||
|
||
return { | ||
bordered, | ||
className: classes, | ||
role, | ||
separated, | ||
...otherProps, | ||
}; | ||
} |
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,50 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { cx } from 'emotion'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { useContextSystem } from '../context'; | ||
// eslint-disable-next-line no-duplicate-imports | ||
import type { PolymorphicComponentProps } from '../context'; | ||
import * as styles from './styles'; | ||
import { useItemGroupContext } from './context'; | ||
|
||
export interface Props { | ||
action?: boolean; | ||
size?: 'small' | 'medium' | 'large'; | ||
} | ||
|
||
export function useItem( props: PolymorphicComponentProps< Props, 'div' > ) { | ||
const { | ||
action = false, | ||
as: asProp, | ||
className, | ||
role = 'listitem', | ||
size: sizeProp, | ||
...otherProps | ||
} = useContextSystem( props, 'Item' ); | ||
|
||
const { spacedAround, size: contextSize } = useItemGroupContext(); | ||
|
||
const size = sizeProp || contextSize; | ||
|
||
const as = asProp || action ? 'button' : 'div'; | ||
|
||
const classes = cx( | ||
action && styles.unstyledButton, | ||
styles.itemSizes[ size ] || styles.itemSizes.medium, | ||
styles.item, | ||
spacedAround && styles.spacedAround, | ||
className | ||
); | ||
|
||
return { | ||
as, | ||
className: classes, | ||
role, | ||
...otherProps, | ||
}; | ||
} |
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
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.
Shall we give this a default value in the context, just for consistency?
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.
We could! I wasn't sure how to go about this, since it's kind of inferred from
bordered
/separated
🤔