-
Notifications
You must be signed in to change notification settings - Fork 11
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
feat: add Table component and styling #238
Open
karrui
wants to merge
13
commits into
main
Choose a base branch
from
feat/table/table-component
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
08e3d89
feat: add initial Table theme
karrui 7d50f8c
feat: add initial Th styles that account for interactivity
karrui 7cfa427
feat: tighten omitInteractivePseudos util typing
karrui 85b050e
style(Th): add neutral colorScheme theming
karrui c68a5fc
refactor: fixup omitInteractivePseudos again
karrui ff0f844
feat: update responsive padding sizes for Table components
karrui 2615137
feat: add neutral color scheme to Table
karrui ba778fc
feat: add interactable Table header story
karrui eaf72ab
feat: reexport Table and subcomponents from package
karrui a7b90b6
feat: update ColourTable to new Tables
karrui 6fa7e07
feat: update Utilities to new Tables
karrui 41bfe74
feat: update Menu playground stories to new Tables
karrui 0a34aa7
feat: update table styles
karrui 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { | ||
Table, | ||
TableCaption, | ||
TableContainer, | ||
TableProps, | ||
Tbody, | ||
Td, | ||
Tfoot, | ||
Thead, | ||
Tr, | ||
} from '@chakra-ui/react' | ||
import { Meta, StoryFn } from '@storybook/react' | ||
|
||
import { Th } from './Th' | ||
|
||
export default { | ||
title: 'Components/Table', | ||
component: Table, | ||
decorators: [], | ||
tags: ['autodocs'], | ||
} as Meta<TableProps> | ||
|
||
const Template: StoryFn<TableProps> = (args) => { | ||
return ( | ||
<TableContainer> | ||
<Table {...args}> | ||
<TableCaption>Imperial to metric conversion factors</TableCaption> | ||
<Thead> | ||
<Tr> | ||
<Th>To convert</Th> | ||
<Th>into</Th> | ||
<Th isNumeric>multiply by</Th> | ||
</Tr> | ||
</Thead> | ||
<Tbody> | ||
<Tr> | ||
<Td>inches</Td> | ||
<Td>millimetres (mm)</Td> | ||
<Td isNumeric>25.4</Td> | ||
</Tr> | ||
<Tr> | ||
<Td>feet</Td> | ||
<Td>centimetres (cm)</Td> | ||
<Td isNumeric>30.48</Td> | ||
</Tr> | ||
<Tr> | ||
<Td>yards</Td> | ||
<Td>metres (m)</Td> | ||
<Td isNumeric>0.91444</Td> | ||
</Tr> | ||
</Tbody> | ||
<Tfoot> | ||
<Tr> | ||
<Th>To convert</Th> | ||
<Th>into</Th> | ||
<Th isNumeric>multiply by</Th> | ||
</Tr> | ||
</Tfoot> | ||
</Table> | ||
</TableContainer> | ||
) | ||
} | ||
export const Default = Template.bind({}) | ||
|
||
export const NeutralColorScheme = Template.bind({}) | ||
NeutralColorScheme.args = { | ||
colorScheme: 'neutral', | ||
} | ||
|
||
export const InteractableHeaders: StoryFn<TableProps> = (args) => { | ||
return ( | ||
<TableContainer> | ||
<Table {...args}> | ||
<Thead> | ||
<Tr> | ||
<Th onClick={() => alert('Column 1 clicked')}>Interactable</Th> | ||
<Th>Not interactable</Th> | ||
</Tr> | ||
</Thead> | ||
<Tbody> | ||
<Tr> | ||
<Td>inches</Td> | ||
<Td>millimetres (mm)</Td> | ||
</Tr> | ||
<Tr> | ||
<Td>feet</Td> | ||
<Td>centimetres (cm)</Td> | ||
</Tr> | ||
<Tr> | ||
<Td>yards</Td> | ||
<Td>metres (m)</Td> | ||
</Tr> | ||
</Tbody> | ||
</Table> | ||
</TableContainer> | ||
) | ||
} |
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,19 @@ | ||
import { TableColumnHeaderProps, useTableStyles } from '@chakra-ui/react' | ||
import { chakra, forwardRef } from '@chakra-ui/system' | ||
|
||
import { omitInteractivePseudos } from './utils/omitInteractivePseudos' | ||
|
||
export const Th = forwardRef<TableColumnHeaderProps, 'th'>( | ||
({ isNumeric, ...props }, ref) => { | ||
const styles = useTableStyles() | ||
const thStyles = omitInteractivePseudos(props, styles.th) | ||
return ( | ||
<chakra.th | ||
{...props} | ||
ref={ref} | ||
__css={thStyles} | ||
data-is-numeric={isNumeric} | ||
/> | ||
) | ||
}, | ||
) |
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,14 @@ | ||
import { | ||
Table, | ||
TableCaption, | ||
TableContainer, | ||
Tbody, | ||
Td, | ||
Tfoot, | ||
Thead, | ||
Tr, | ||
} from '@chakra-ui/react' | ||
|
||
export * from './Th' | ||
|
||
export { Table, TableCaption, TableContainer, Tbody, Td, Tfoot, Thead, Tr } |
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,54 @@ | ||
import { DOMAttributes } from 'react' | ||
import { SystemStyleObject } from '@chakra-ui/react' | ||
import { omit } from 'lodash' | ||
|
||
type InteractableProps = Pick< | ||
DOMAttributes<unknown>, | ||
| 'onClick' | ||
| 'onKeyDown' | ||
| 'onKeyUp' | ||
| 'onKeyPress' | ||
| 'onMouseDown' | ||
| 'onMouseUp' | ||
| 'onMouseEnter' | ||
| 'onMouseLeave' | ||
| 'onMouseMove' | ||
| 'onMouseOver' | ||
| 'onMouseOut' | ||
| 'onFocus' | ||
| 'onBlur' | ||
> | ||
|
||
const isInteractive = (props: InteractableProps) => { | ||
return ( | ||
!!props.onClick || | ||
!!props.onKeyDown || | ||
!!props.onKeyUp || | ||
!!props.onKeyPress || | ||
!!props.onMouseDown || | ||
!!props.onMouseUp || | ||
!!props.onMouseEnter || | ||
!!props.onMouseLeave || | ||
!!props.onMouseMove || | ||
!!props.onMouseOver || | ||
!!props.onMouseOut || | ||
!!props.onFocus || | ||
!!props.onBlur | ||
) | ||
} | ||
|
||
const INTERACTIVE_PSEUDO_PROPS = ['_active', '_hover', '_pressed', '_selected'] | ||
|
||
/** | ||
* Function to omit interactive pseudos from a style object if the object is not | ||
* interactive (i.e. if it does not have an `onClick`, `onKeyDown` props, etc) | ||
*/ | ||
export function omitInteractivePseudos( | ||
props: InteractableProps, | ||
styles: SystemStyleObject, | ||
): SystemStyleObject { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: look like low hanging fruit to do |
||
if (!isInteractive(props)) { | ||
return omit(styles, INTERACTIVE_PSEUDO_PROPS) | ||
} | ||
return styles | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
import { tableAnatomy as parts } from '@chakra-ui/anatomy' | ||
import { | ||
createMultiStyleConfigHelpers, | ||
SystemStyleObject, | ||
} from '@chakra-ui/react' | ||
|
||
import { layerStyles } from '../layerStyles' | ||
import { textStyles } from '../textStyles' | ||
|
||
const { defineMultiStyleConfig, definePartsStyle } = | ||
createMultiStyleConfigHelpers(parts.keys) | ||
|
||
const baseStyle = definePartsStyle({ | ||
tr: { | ||
pos: 'relative', | ||
}, | ||
}) | ||
|
||
const sizes = { | ||
xs: definePartsStyle({ | ||
th: { | ||
py: '0.875rem', | ||
}, | ||
td: { | ||
py: '0.875rem', | ||
px: '1rem', | ||
textStyle: 'caption-2', | ||
}, | ||
}), | ||
sm: definePartsStyle({ | ||
th: { | ||
...textStyles['subhead-2'], | ||
py: '0.875rem', | ||
}, | ||
td: { | ||
py: '0.875rem', | ||
px: '1rem', | ||
textStyle: 'caption-2', | ||
}, | ||
}), | ||
md: { | ||
th: { | ||
py: '1rem', | ||
}, | ||
td: { | ||
textStyle: 'body-2', | ||
p: '1rem', | ||
}, | ||
}, | ||
lg: { | ||
th: { | ||
py: '1.125rem', | ||
}, | ||
td: { | ||
textStyle: 'body-2', | ||
px: '1rem', | ||
py: '1.125rem', | ||
}, | ||
}, | ||
} | ||
|
||
const getSubtleVariantThStyles = (c: string): SystemStyleObject => { | ||
const baseStyles: SystemStyleObject = { | ||
textTransform: 'initial', | ||
_focusVisible: { | ||
...layerStyles.focusRing.tightDefault._focusVisible, | ||
outlineOffset: '-2px', | ||
}, | ||
} | ||
|
||
switch (c) { | ||
case 'main': | ||
case 'sub': | ||
case 'critical': | ||
case 'warning': | ||
case 'success': { | ||
return { | ||
bg: `interaction.${c}-subtle.default`, | ||
color: `interaction.${c}.default`, | ||
_hover: { | ||
bg: `interaction.${c}-subtle.hover`, | ||
cursor: 'pointer', | ||
}, | ||
_active: { | ||
bg: `interaction.${c}-subtle.active`, | ||
}, | ||
...baseStyles, | ||
} | ||
} | ||
case 'neutral': { | ||
return { | ||
bg: `interaction.${c}-subtle.default`, | ||
color: 'base.content.medium', | ||
_hover: { | ||
bg: `interaction.${c}-subtle.hover`, | ||
cursor: 'pointer', | ||
}, | ||
_active: { | ||
bg: `interaction.${c}-subtle.active`, | ||
}, | ||
...baseStyles, | ||
} | ||
} | ||
default: { | ||
return { | ||
bg: `interaction.main-subtle.default`, | ||
...baseStyles, | ||
} | ||
} | ||
} | ||
} | ||
|
||
const variantSubtle = definePartsStyle(({ colorScheme: c }) => { | ||
return { | ||
thead: { | ||
bg: `interaction.${c}-subtle.default`, | ||
opacity: 1, | ||
zIndex: 1, | ||
}, | ||
th: getSubtleVariantThStyles(c), | ||
td: { | ||
color: 'base.content.default', | ||
}, | ||
} | ||
}) | ||
|
||
const variants = { | ||
subtle: variantSubtle, | ||
} | ||
|
||
export const Table = defineMultiStyleConfig({ | ||
baseStyle, | ||
variants, | ||
defaultProps: { | ||
variant: 'subtle', | ||
size: 'md', | ||
colorScheme: 'main', | ||
}, | ||
sizes, | ||
}) |
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.
is
_focus
not included here? from mdn docs, it seems to be interactable (also, should we base this off the mdn one?)