Skip to content

Commit

Permalink
feat(popover): add inset prop to popover
Browse files Browse the repository at this point in the history
  • Loading branch information
soykje committed Nov 21, 2023
1 parent 2d3954c commit a3482e4
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 1 deletion.
6 changes: 6 additions & 0 deletions packages/components/popover/src/Popover.doc.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ The content is the popover itself.

<Canvas of={stories.Default} />

### Inset

When set to `true`, this option will remove the internal padding of the `Popover.Content` component. This allows you to have an image or any content that occupies the full width, for example.

<Canvas of={stories.Inset} />

### Uncontrolled

By default, `Popover` doesn't need external state to open/close itself.
Expand Down
19 changes: 19 additions & 0 deletions packages/components/popover/src/Popover.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,25 @@ export const Default: StoryFn = _args => {
)
}

export const Inset: StoryFn = _args => {
return (
<ShowcaseContainer>
<Popover>
<Popover.Trigger asChild>
<Button>Trigger popover</Button>
</Popover.Trigger>
<Popover.Portal>
<Popover.Content inset>
<img src="https://placehold.co/300x200/white/grey" alt="" />
<Popover.Arrow />
<Popover.CloseButton aria-label="Close the popover" />
</Popover.Content>
</Popover.Portal>
</Popover>
</ShowcaseContainer>
)
}

export const Uncontrolled: StoryFn = () => {
return (
<ShowcaseContainer>
Expand Down
16 changes: 16 additions & 0 deletions packages/components/popover/src/Popover.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe('Popover', () => {
beforeAll(() => {
mockResizeObserver()
})

describe('opening', () => {
it('should open popover on click', async () => {
const user = userEvent.setup()
Expand Down Expand Up @@ -351,4 +352,19 @@ describe('Popover', () => {
expect(within(originalContainer).queryByText('Popover content')).not.toBeInTheDocument()
expect(screen.getByText('Popover content')).toBeInTheDocument()
})

it('should handle the inset prop', async () => {
render(
<Popover open>
<Popover.Trigger asChild>
<button type="button">Click me</button>
</Popover.Trigger>
<Popover.Portal>
<Popover.Content inset>Popover content</Popover.Content>
</Popover.Portal>
</Popover>
)

expect(screen.getByText(/Popover content/i)).not.toHaveClass('p-lg')
})
})
13 changes: 12 additions & 1 deletion packages/components/popover/src/PopoverContent.styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ export const styles = cva(
[
'z-popover',
'rounded-md',
'p-lg',
'bg-surface text-on-surface',
'shadow',
'focus-visible:outline-none focus-visible:u-ring',
Expand All @@ -24,11 +23,23 @@ export const styles = cva(
hasCloseButton: {
true: 'pr-[40px]',
},
inset: {
true: 'overflow-hidden',
false: 'p-lg',
},
},
compoundVariants: [
{
hasCloseButton: true,
inset: true,
class: 'pr-none',
},
],
defaultVariants: {
matchTriggerWidth: false,
enforceBoundaries: false,
hasCloseButton: false,
inset: false,
},
}
)
Expand Down
2 changes: 2 additions & 0 deletions packages/components/popover/src/PopoverContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const Content = forwardRef<HTMLDivElement, ContentProps>(
side = 'bottom',
sideOffset = 8,
sticky = 'partial',
inset = false,
...rest
},
ref
Expand All @@ -38,6 +39,7 @@ export const Content = forwardRef<HTMLDivElement, ContentProps>(
enforceBoundaries: !!collisionBoundary,
matchTriggerWidth,
hasCloseButton,
inset,
className,
})}
data-spark-component="popover-content"
Expand Down

0 comments on commit a3482e4

Please sign in to comment.