-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ToggleButtonGroup component: Add support for variant and backgroundCo…
…lor (#824)
- Loading branch information
Showing
8 changed files
with
264 additions
and
20 deletions.
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
9 changes: 9 additions & 0 deletions
9
packages/react-ui/src/components/atoms/ToggleButtonGroup.d.ts
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,9 @@ | ||
import { ToggleButtonGroupProps as MuiToggleButtonGroupProps } from '@mui/material'; | ||
|
||
export type ToggleButtonGroupProps = MuiToggleButtonGroupProps & { | ||
variant?: 'contained' | 'floating' | 'unbounded'; | ||
backgroundColor?: 'primary' | 'secondary' | 'transparent'; | ||
}; | ||
|
||
declare const ToggleButtonGroup: (props: ToggleButtonGroupProps) => JSX.Element; | ||
export default ToggleButtonGroup; |
110 changes: 110 additions & 0 deletions
110
packages/react-ui/src/components/atoms/ToggleButtonGroup.js
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,110 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { ToggleButtonGroup as MuiToggleButtonGroup, styled } from '@mui/material'; | ||
|
||
const StyledToggleButtonGroup = styled(MuiToggleButtonGroup, { | ||
shouldForwardProp: (prop) => !['variant', 'backgroundColor'].includes(prop) | ||
})(({ variant, backgroundColor, theme }) => ({ | ||
// Variants | ||
...(variant === 'contained' && { | ||
boxShadow: 'none' | ||
}), | ||
...(variant === 'unbounded' && { | ||
boxShadow: 'none', | ||
borderRadius: theme.spacing(0.5), | ||
|
||
'& .MuiDivider-root': { | ||
height: theme.spacing(4), | ||
|
||
'&.MuiToggleButtonGroup-groupedHorizontal': { | ||
height: theme.spacing(4) | ||
}, | ||
'&.MuiToggleButtonGroup-groupedVertical': { | ||
height: 'auto', | ||
width: theme.spacing(4), | ||
margin: `${theme.spacing(0.5, 0, 1)} !important`, | ||
borderRadius: '0 !important' | ||
} | ||
}, | ||
|
||
'& .MuiToggleButton-sizeSmall': { | ||
margin: 0, | ||
|
||
'&.MuiToggleButtonGroup-grouped:not(.MuiDivider-root)': { | ||
margin: 0 | ||
}, | ||
'& + .MuiDivider-root.MuiToggleButtonGroup-groupedHorizontal': { | ||
height: theme.spacing(3) | ||
}, | ||
'& + .MuiDivider-root.MuiToggleButtonGroup-groupedVertical': { | ||
height: 'auto', | ||
width: theme.spacing(3) | ||
} | ||
}, | ||
|
||
'.MuiToggleButtonGroup-grouped:not(.MuiDivider-root)': { | ||
margin: 0, | ||
|
||
'&:first-of-type': { | ||
marginLeft: 0 | ||
}, | ||
'&:not(:last-of-type)': { | ||
marginRight: theme.spacing(0.5) | ||
} | ||
}, | ||
'&.MuiToggleButtonGroup-horizontal:not(.MuiDivider-root)': { | ||
'.MuiToggleButtonGroup-grouped': { | ||
margin: theme.spacing(0, 0.5) | ||
} | ||
}, | ||
'&.MuiToggleButtonGroup-vertical:not(.MuiDivider-root)': { | ||
'.MuiToggleButtonGroup-grouped': { | ||
margin: theme.spacing(0, 0, 0.5), | ||
|
||
'&:not(:last-of-type)': { | ||
marginRight: 0 | ||
}, | ||
'&:last-of-type': { | ||
marginBottom: 0 | ||
} | ||
} | ||
} | ||
}), | ||
|
||
// Colors | ||
...(backgroundColor === 'primary' && { | ||
backgroundColor: theme.palette.background.paper | ||
}), | ||
...(backgroundColor === 'secondary' && { | ||
backgroundColor: theme.palette.background.default | ||
}), | ||
...(backgroundColor === 'transparent' && { | ||
backgroundColor: 'transparent' | ||
}) | ||
})); | ||
|
||
const ToggleButtonGroup = ({ children, variant, backgroundColor, ...rest }) => { | ||
const isUnbounded = variant === 'unbounded'; | ||
const defaultColor = isUnbounded ? 'transparent' : 'primary'; | ||
|
||
return ( | ||
<StyledToggleButtonGroup | ||
{...rest} | ||
variant={variant} | ||
backgroundColor={backgroundColor || defaultColor} | ||
> | ||
{children} | ||
</StyledToggleButtonGroup> | ||
); | ||
}; | ||
|
||
ToggleButtonGroup.defaultProps = { | ||
variant: 'floating' | ||
}; | ||
|
||
ToggleButtonGroup.propTypes = { | ||
variant: PropTypes.oneOf(['floating', 'contained', 'unbounded']), | ||
backgroundColor: PropTypes.oneOf(['primary', 'secondary', 'transparent']) | ||
}; | ||
|
||
export default ToggleButtonGroup; |
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
Oops, something went wrong.