mirror of
https://github.com/carbon-design-system/carbon-components-svelte.git
synced 2025-09-18 11:36:36 +00:00
feat(popover): add closeOnOutsideClick prop
This commit is contained in:
parent
4397d852eb
commit
b728454a60
6 changed files with 81 additions and 12 deletions
|
@ -2640,8 +2640,9 @@ None.
|
|||
### Props
|
||||
|
||||
| Prop name | Kind | Reactive | Type | Default value | Description |
|
||||
| :----------- | :--------------- | :------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------ | ------------------------------------------------- |
|
||||
| open | <code>let</code> | No | <code>boolean</code> | <code>false</code> | Set to `true` to display the popover |
|
||||
| :------------------ | :--------------- | :------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------ | ------------------------------------------------------ |
|
||||
| open | <code>let</code> | Yes | <code>boolean</code> | <code>false</code> | Set to `true` to display the popover |
|
||||
| closeOnOutsideClick | <code>let</code> | No | <code>boolean</code> | <code>false</code> | Set to `true` to close the popover on an outside click |
|
||||
| caret | <code>let</code> | No | <code>boolean</code> | <code>false</code> | Set to `true` render a caret |
|
||||
| align | <code>let</code> | No | <code>"top" | "top-left" | "top-right" | "bottom" | "bottom-left" | "bottom-right" | "left" | "left-bottom" | "left-top" | "right" | "right-bottom" | "right-top"</code> | <code>"top"</code> | Specify the alignment of the caret |
|
||||
| light | <code>let</code> | No | <code>boolean</code> | <code>false</code> | Set to `true` to enable the light variant |
|
||||
|
@ -2656,7 +2657,9 @@ None.
|
|||
|
||||
### Events
|
||||
|
||||
None.
|
||||
| Event name | Type | Detail |
|
||||
| :------------ | :--------- | :----- |
|
||||
| click:outside | dispatched | -- |
|
||||
|
||||
## `ProgressIndicator`
|
||||
|
||||
|
|
|
@ -6800,6 +6800,16 @@
|
|||
"value": "false",
|
||||
"isFunction": false,
|
||||
"constant": false,
|
||||
"reactive": true
|
||||
},
|
||||
{
|
||||
"name": "closeOnOutsideClick",
|
||||
"kind": "let",
|
||||
"description": "Set to `true` to close the popover on an outside click",
|
||||
"type": "boolean",
|
||||
"value": "false",
|
||||
"isFunction": false,
|
||||
"constant": false,
|
||||
"reactive": false
|
||||
},
|
||||
{
|
||||
|
@ -6854,7 +6864,7 @@
|
|||
}
|
||||
],
|
||||
"slots": [{ "name": "__default__", "default": true, "slot_props": "{}" }],
|
||||
"events": [],
|
||||
"events": [{ "type": "dispatched", "name": "click:outside" }],
|
||||
"typedefs": [],
|
||||
"rest_props": { "type": "Element", "name": "div" }
|
||||
},
|
||||
|
|
|
@ -14,7 +14,7 @@ By default, the position of the popover component is absolute.
|
|||
</Popover>
|
||||
</div>
|
||||
|
||||
### Relative
|
||||
### Relative position
|
||||
|
||||
Set `relative` to `true` to use a relative position.
|
||||
|
||||
|
@ -25,6 +25,16 @@ Set `relative` to `true` to use a relative position.
|
|||
</Popover>
|
||||
</div>
|
||||
|
||||
### Close on outside click
|
||||
|
||||
Set `closeOnOutsideClick` to set `open` to `false` when clicking outside of the popover.
|
||||
|
||||
<div data-outline="relative">
|
||||
Parent
|
||||
<Popover open closeOnOutsideClick on:click:outside={() => {console.log('on:click:outside')}}>
|
||||
<div style="padding: var(--cds-spacing-05)">Content</div>
|
||||
</Popover>
|
||||
</div>
|
||||
|
||||
### With caret
|
||||
|
||||
|
|
|
@ -2,6 +2,9 @@
|
|||
/** Set to `true` to display the popover */
|
||||
export let open = false;
|
||||
|
||||
/** Set to `true` to close the popover on an outside click */
|
||||
export let closeOnOutsideClick = false;
|
||||
|
||||
/** Set to `true` render a caret */
|
||||
export let caret = false;
|
||||
|
||||
|
@ -19,9 +22,26 @@
|
|||
|
||||
/** Set to `true` to use a relative position */
|
||||
export let relative = false;
|
||||
|
||||
import { createEventDispatcher } from "svelte";
|
||||
|
||||
const dispatch = createEventDispatcher();
|
||||
|
||||
let ref = null;
|
||||
</script>
|
||||
|
||||
<svelte:window
|
||||
on:click="{(e) => {
|
||||
if (!open) return;
|
||||
if (e.target.contains(ref)) {
|
||||
dispatch('click:outside');
|
||||
if (closeOnOutsideClick) open = false;
|
||||
}
|
||||
}}"
|
||||
/>
|
||||
|
||||
<div
|
||||
bind:this="{ref}"
|
||||
class:bx--popover="{true}"
|
||||
class:bx--popover--caret="{caret}"
|
||||
class:bx--popover--light="{light}"
|
||||
|
|
20
tests/Popover.test.svelte
Normal file
20
tests/Popover.test.svelte
Normal file
|
@ -0,0 +1,20 @@
|
|||
<script lang="ts">
|
||||
import { Popover } from "../types";
|
||||
|
||||
let open = false;
|
||||
</script>
|
||||
|
||||
<Popover
|
||||
bind:open
|
||||
closeOnOutsideClick
|
||||
align="right"
|
||||
caret
|
||||
relative
|
||||
light
|
||||
highContrast
|
||||
on:click:outside="{() => {
|
||||
console.log('on:click:outside');
|
||||
}}"
|
||||
>
|
||||
<div style="padding: var(--cds-spacing-05)">Content</div>
|
||||
</Popover>
|
8
types/Popover/Popover.d.ts
vendored
8
types/Popover/Popover.d.ts
vendored
|
@ -9,6 +9,12 @@ export interface PopoverProps
|
|||
*/
|
||||
open?: boolean;
|
||||
|
||||
/**
|
||||
* Set to `true` to close the popover on an outside click
|
||||
* @default false
|
||||
*/
|
||||
closeOnOutsideClick?: boolean;
|
||||
|
||||
/**
|
||||
* Set to `true` render a caret
|
||||
* @default false
|
||||
|
@ -54,6 +60,6 @@ export interface PopoverProps
|
|||
|
||||
export default class Popover extends SvelteComponentTyped<
|
||||
PopoverProps,
|
||||
{},
|
||||
{ ["click:outside"]: CustomEvent<any> },
|
||||
{ default: {} }
|
||||
> {}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue