Skip to content

Commit

Permalink
Add minWidth prop to PageLayout.Pane (#3229)
Browse files Browse the repository at this point in the history
* Add minWidth prop to PageLayout.Pane

* Create late-wombats-switch.md

* Update generated/components.json

* Update playground stories

---------

Co-authored-by: colebemis <colebemis@users.noreply.github.com>
  • Loading branch information
colebemis and colebemis committed May 1, 2023
1 parent 66995dc commit c0cbdd0
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/late-wombats-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@primer/react": minor
---

Add `minWidth prop to `PageLayout.Pane` and `SplitPageLayout.Pane`
12 changes: 12 additions & 0 deletions generated/components.json
Original file line number Diff line number Diff line change
Expand Up @@ -2723,6 +2723,12 @@
"defaultValue": "'medium'",
"description": "The width of the pane."
},
{
"name": "minWidth",
"type": "number",
"defaultValue": "256",
"description": "The minimum width of the pane."
},
{
"name": "resizable",
"type": "boolean",
Expand Down Expand Up @@ -3720,6 +3726,12 @@
"defaultValue": "'medium'",
"description": "The width of the pane."
},
{
"name": "minWidth",
"type": "number",
"defaultValue": "256",
"description": "The minimum width of the pane."
},
{
"name": "resizable",
"type": "boolean",
Expand Down
6 changes: 6 additions & 0 deletions src/PageLayout/PageLayout.docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@
"defaultValue": "'medium'",
"description": "The width of the pane."
},
{
"name": "minWidth",
"type": "number",
"defaultValue": "256",
"description": "The minimum width of the pane."
},
{
"name": "resizable",
"type": "boolean",
Expand Down
6 changes: 6 additions & 0 deletions src/PageLayout/PageLayout.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,11 @@ const meta: Meta = {
control: {type: 'radio'},
table: {category: 'Pane props'},
},
'Pane.minWidth': {
type: 'number',
defaultValue: 256,
table: {category: 'Pane props'},
},
'Pane.sticky': {
type: 'boolean',
table: {category: 'Pane props'},
Expand Down Expand Up @@ -383,6 +388,7 @@ const Template: Story = args => (
wide: args['Pane.position.wide'],
}}
width={args['Pane.width']}
minWidth={args['Pane.minWidth']}
sticky={args['Pane.sticky']}
resizable={args['Pane.resizable']}
padding={args['Pane.padding']}
Expand Down
11 changes: 6 additions & 5 deletions src/PageLayout/PageLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -482,10 +482,11 @@ export type PageLayoutPaneProps = {
* position={{regular: 'start', narrow: 'end'}}
* ```
*/
positionWhenNarrow?: 'inherit' | keyof typeof panePositions
'aria-labelledby'?: string
'aria-label'?: string
positionWhenNarrow?: 'inherit' | keyof typeof panePositions
width?: keyof typeof paneWidths
minWidth?: number
resizable?: boolean
widthStorageKey?: string
padding?: keyof typeof SPACING_MAP
Expand Down Expand Up @@ -532,6 +533,7 @@ const Pane = React.forwardRef<HTMLDivElement, React.PropsWithChildren<PageLayout
position: responsivePosition = 'end',
positionWhenNarrow = 'inherit',
width = 'medium',
minWidth = 256,
padding = 'none',
resizable = false,
widthStorageKey = 'paneWidth',
Expand Down Expand Up @@ -603,7 +605,6 @@ const Pane = React.forwardRef<HTMLDivElement, React.PropsWithChildren<PageLayout
const paneRef = React.useRef<HTMLDivElement>(null)
useRefObjectAsForwardedRef(forwardRef, paneRef)

const MIN_PANE_WIDTH = 256 // 256px, related to `--pane-min-width CSS var.
const [minPercent, setMinPercent] = React.useState(0)
const [maxPercent, setMaxPercent] = React.useState(0)
const hasOverflow = useOverflow(paneRef)
Expand All @@ -618,7 +619,7 @@ const Pane = React.forwardRef<HTMLDivElement, React.PropsWithChildren<PageLayout
const viewportWidth = window.innerWidth
const maxPaneWidth = viewportWidth > maxPaneWidthDiff ? viewportWidth - maxPaneWidthDiff : viewportWidth

const minPercent = Math.round((100 * MIN_PANE_WIDTH) / viewportWidth)
const minPercent = Math.round((100 * minWidth) / viewportWidth)
setMinPercent(minPercent)

const maxPercent = Math.round((100 * maxPaneWidth) / viewportWidth)
Expand All @@ -627,7 +628,7 @@ const Pane = React.forwardRef<HTMLDivElement, React.PropsWithChildren<PageLayout
const widthPercent = Math.round((100 * paneWidth) / viewportWidth)
setWidthPercent(widthPercent.toString())
}
}, [paneRef])
}, [paneRef, minWidth])

const [widthPercent, setWidthPercent] = React.useState('')
const [prevPercent, setPrevPercent] = React.useState('')
Expand Down Expand Up @@ -742,7 +743,7 @@ const Pane = React.forwardRef<HTMLDivElement, React.PropsWithChildren<PageLayout
'--pane-width': `${paneWidth}px`,
}}
sx={(theme: Theme) => ({
'--pane-min-width': `256px`,
'--pane-min-width': `${minWidth}px`,
'--pane-max-width-diff': '511px',
'--pane-max-width': `calc(100vw - var(--pane-max-width-diff))`,
width: resizable
Expand Down
8 changes: 7 additions & 1 deletion src/SplitPageLayout/SplitPageLayout.docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@
"defaultValue": "'medium'",
"description": "The width of the pane."
},
{
"name": "minWidth",
"type": "number",
"defaultValue": "256",
"description": "The minimum width of the pane."
},
{
"name": "resizable",
"type": "boolean",
Expand Down Expand Up @@ -156,4 +162,4 @@
]
}
]
}
}
6 changes: 6 additions & 0 deletions src/SplitPageLayout/SplitPageLayout.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ export default {
control: {type: 'radio'},
table: {category: 'Pane props'},
},
'Pane.minWidth': {
type: 'number',
defaultValue: 256,
table: {category: 'Pane props'},
},
'Pane.sticky': {
type: 'boolean',
defaultValue: true,
Expand Down Expand Up @@ -342,6 +347,7 @@ const Template: Story = args => (
wide: args['Pane.position.wide'],
}}
width={args['Pane.width']}
minWidth={args['Pane.minWidth']}
sticky={args['Pane.sticky']}
padding={args['Pane.padding']}
divider={{
Expand Down

0 comments on commit c0cbdd0

Please sign in to comment.