mirror of
https://github.com/carbon-design-system/carbon-components-svelte.git
synced 2025-09-18 11:36:36 +00:00
Fix popover click outside handler when clicking siblings
This commit is contained in:
parent
ae34451583
commit
3a52b5b903
4 changed files with 46 additions and 3 deletions
|
@ -36,6 +36,12 @@ Set `closeOnOutsideClick` to set `open` to `false` when clicking outside of the
|
|||
</Popover>
|
||||
</div>
|
||||
|
||||
## Manage open state with a Button
|
||||
|
||||
You can bind the `open` prop and manage the open state of the popover with an external button.
|
||||
|
||||
<FileSource src="/framed/Popover/WithButton" />
|
||||
|
||||
## Popover alignment
|
||||
|
||||
Customize the popover alignment using the `align` prop. Valid values include: `"top" | "top-left" | "top-right" | "bottom" | "bottom-left" | "bottom-right" | "left" | "left-bottom" | "left-top" | "right" | "right-bottom" | "right-top"`.
|
||||
|
|
37
docs/src/pages/framed/Popover/WithButton.svelte
Normal file
37
docs/src/pages/framed/Popover/WithButton.svelte
Normal file
|
@ -0,0 +1,37 @@
|
|||
<script>
|
||||
import { Popover, Button } from "carbon-components-svelte";
|
||||
|
||||
let open = true;
|
||||
let containerRef = null;
|
||||
</script>
|
||||
|
||||
<div data-outline>
|
||||
<div
|
||||
bind:this="{containerRef}"
|
||||
style:position="relative"
|
||||
style:display="inline-block"
|
||||
>
|
||||
<Button on:click="{() => (open = !open)}">Click to open</Button>
|
||||
<Popover
|
||||
bind:open
|
||||
align="bottom-left"
|
||||
on:click:outside="{({ detail }) => {
|
||||
console.log('on:click:outside');
|
||||
if (!containerRef.contains(detail.target)) {
|
||||
open = false;
|
||||
}
|
||||
}}"
|
||||
>
|
||||
<div style="padding: var(--cds-spacing-05)">Content</div>
|
||||
</Popover>
|
||||
</div>
|
||||
<div>
|
||||
<br />
|
||||
Anim eu laboris veniam in est elit ullamco enim irure id. Lorem est culpa ex
|
||||
in enim enim. Culpa quis incididunt magna officia et aliqua. Ullamco deserunt
|
||||
aute enim cupidatat. Culpa quis enim sint sint anim qui irure exercitation labore
|
||||
occaecat in reprehenderit proident. Nisi laborum ut et amet commodo amet ullamco
|
||||
commodo non aliqua magna ad eu. Ad aliquip minim nostrud id enim elit velit ullamco
|
||||
pariatur eu excepteur pariatur cillum.
|
||||
</div>
|
||||
</div>
|
|
@ -33,8 +33,8 @@
|
|||
<svelte:window
|
||||
on:click="{(e) => {
|
||||
if (!open) return;
|
||||
if (e.target.contains(ref)) {
|
||||
dispatch('click:outside');
|
||||
if (!ref.contains(e.target)) {
|
||||
dispatch('click:outside', { target: e.target });
|
||||
if (closeOnOutsideClick) open = false;
|
||||
}
|
||||
}}"
|
||||
|
|
2
types/Popover/Popover.svelte.d.ts
vendored
2
types/Popover/Popover.svelte.d.ts
vendored
|
@ -60,6 +60,6 @@ export interface PopoverProps
|
|||
|
||||
export default class Popover extends SvelteComponentTyped<
|
||||
PopoverProps,
|
||||
{ ["click:outside"]: CustomEvent<null> },
|
||||
{ ["click:outside"]: CustomEvent<{ target: HTMLElement }> },
|
||||
{ default: {} }
|
||||
> {}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue