mirror of
https://github.com/carbon-design-system/carbon-components-svelte.git
synced 2025-09-15 02:11:05 +00:00
fix(popover): fire "click:outside" event when clicking sibling elements (#1570)
This commit is contained in:
parent
fa5515ded0
commit
0f8a57b9d2
6 changed files with 58 additions and 7 deletions
|
@ -2805,8 +2805,8 @@ None.
|
||||||
### Events
|
### Events
|
||||||
|
|
||||||
| Event name | Type | Detail |
|
| Event name | Type | Detail |
|
||||||
| :------------ | :--------- | :---------------- |
|
| :------------ | :--------- | :------------------------------------ |
|
||||||
| click:outside | dispatched | <code>null</code> |
|
| click:outside | dispatched | <code>{ target: HTMLElement; }</code> |
|
||||||
|
|
||||||
## `ProgressBar`
|
## `ProgressBar`
|
||||||
|
|
||||||
|
|
|
@ -8908,7 +8908,11 @@
|
||||||
"moduleExports": [],
|
"moduleExports": [],
|
||||||
"slots": [{ "name": "__default__", "default": true, "slot_props": "{}" }],
|
"slots": [{ "name": "__default__", "default": true, "slot_props": "{}" }],
|
||||||
"events": [
|
"events": [
|
||||||
{ "type": "dispatched", "name": "click:outside", "detail": "null" }
|
{
|
||||||
|
"type": "dispatched",
|
||||||
|
"name": "click:outside",
|
||||||
|
"detail": "{ target: HTMLElement; }"
|
||||||
|
}
|
||||||
],
|
],
|
||||||
"typedefs": [],
|
"typedefs": [],
|
||||||
"rest_props": { "type": "Element", "name": "div" }
|
"rest_props": { "type": "Element", "name": "div" }
|
||||||
|
|
|
@ -36,6 +36,12 @@ Set `closeOnOutsideClick` to set `open` to `false` when clicking outside of the
|
||||||
</Popover>
|
</Popover>
|
||||||
</div>
|
</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
|
## 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"`.
|
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>
|
|
@ -1,4 +1,8 @@
|
||||||
<script>
|
<script>
|
||||||
|
/**
|
||||||
|
* @event {{ target: HTMLElement; }} click:outside
|
||||||
|
*/
|
||||||
|
|
||||||
/** Set to `true` to display the popover */
|
/** Set to `true` to display the popover */
|
||||||
export let open = false;
|
export let open = false;
|
||||||
|
|
||||||
|
@ -33,8 +37,8 @@
|
||||||
<svelte:window
|
<svelte:window
|
||||||
on:click="{(e) => {
|
on:click="{(e) => {
|
||||||
if (!open) return;
|
if (!open) return;
|
||||||
if (e.target.contains(ref)) {
|
if (!ref.contains(e.target)) {
|
||||||
dispatch('click:outside');
|
dispatch('click:outside', { target: e.target });
|
||||||
if (closeOnOutsideClick) open = false;
|
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<
|
export default class Popover extends SvelteComponentTyped<
|
||||||
PopoverProps,
|
PopoverProps,
|
||||||
{ ["click:outside"]: CustomEvent<null> },
|
{ ["click:outside"]: CustomEvent<{ target: HTMLElement }> },
|
||||||
{ default: {} }
|
{ default: {} }
|
||||||
> {}
|
> {}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue