Skip to content

Commit

Permalink
Add clickoutside action
Browse files Browse the repository at this point in the history
  • Loading branch information
f-elix committed May 14, 2024
1 parent 7f9525d commit ef47ca8
Show file tree
Hide file tree
Showing 15 changed files with 84 additions and 7 deletions.
8 changes: 8 additions & 0 deletions packages/components/back-to-top/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# @288-toolkit/back-to-top

## 3.0.3

### Patch Changes

- Updated dependencies
- Updated dependencies
- @288-toolkit/ui@5.2.0

## 3.0.2

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/components/back-to-top/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@288-toolkit/back-to-top",
"version": "3.0.2",
"version": "3.0.3",
"author": "DeuxHuitHuit",
"license": "MIT",
"type": "module",
Expand Down
9 changes: 9 additions & 0 deletions packages/components/pagination/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# @288-toolkit/pagination

## 3.0.13

### Patch Changes

- Updated dependencies
- Updated dependencies
- @288-toolkit/ui@5.2.0
- @288-toolkit/forms@4.1.1

## 3.0.12

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/components/pagination/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@288-toolkit/pagination",
"version": "3.0.12",
"version": "3.0.13",
"author": "DeuxHuitHuit",
"license": "MIT",
"type": "module",
Expand Down
6 changes: 6 additions & 0 deletions packages/forms/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @288-toolkit/forms

## 4.1.1

### Patch Changes

- Add clickoutside action

## 4.1.0

### Minor Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/forms/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@288-toolkit/forms",
"version": "4.1.0",
"version": "4.1.1",
"author": "DeuxHuitHuit",
"license": "MIT",
"type": "module",
Expand Down
7 changes: 7 additions & 0 deletions packages/ui/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @288-toolkit/ui

## 5.2.0

### Minor Changes

- Add clickOutside action
- Add clickoutside action

## 5.1.0

### Minor Changes
Expand Down
18 changes: 15 additions & 3 deletions packages/ui/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,25 @@ const fontSizeFromInitials: (initials: string, fontSizes: readonly string[]) =>

## `portal`

Renders an element in a different part of the DOM. Accepts a css selector or an `HTMLElement` as the
target.
Renders the element in a different part of the DOM. Accepts a css selector or an `HTMLElement` as
the target.

Based on https://github.com/romkor/svelte-portal/tree/master

```svelte
<div use:portal={'css selector'}>
<div use:portal={'css selector'}></div>
```

## `clickOutside`

Run a callback when a click outside of the element occurs.

```svelte
<div
use:clickOutside={() => {
console.log('clicked outside!');
}}
></div>
```

## `scrollIntoView`
Expand Down
3 changes: 3 additions & 0 deletions packages/ui/dist/clickOutside.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export declare const clickOutside: (node: HTMLElement, callback: (event: PointerEvent) => void) => {
destroy: any;
};
13 changes: 13 additions & 0 deletions packages/ui/dist/clickOutside.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { clickUseCapture } from '@288-toolkit/device/window';
export const clickOutside = (node, callback) => {
const handleClick = (event) => {
if (node.contains(event.target)) {
return;
}
callback(event);
};
const destroy = clickUseCapture.subscribe(handleClick);
return {
destroy
};
};
1 change: 1 addition & 0 deletions packages/ui/dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from './autofocus.js';
export * from './autofocusAfterTick.js';
export * from './autofocusWithDelay.js';
export * from './clickOutside.js';
export * from './focalPointToObjectPosition.js';
export * from './focusableSelector.js';
export * from './imageSizes.js';
Expand Down
1 change: 1 addition & 0 deletions packages/ui/dist/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from './autofocus.js';
export * from './autofocusAfterTick.js';
export * from './autofocusWithDelay.js';
export * from './clickOutside.js';
export * from './focalPointToObjectPosition.js';
export * from './focusableSelector.js';
export * from './imageSizes.js';
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@288-toolkit/ui",
"version": "5.1.0",
"version": "5.2.0",
"author": "DeuxHuitHuit",
"license": "MIT",
"type": "module",
Expand Down
16 changes: 16 additions & 0 deletions packages/ui/src/clickOutside.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { clickUseCapture } from '@288-toolkit/device/window';

export const clickOutside = (node: HTMLElement, callback: (event: PointerEvent) => void) => {
const handleClick = (event: PointerEvent) => {
if (node.contains(event.target as HTMLElement)) {
return;
}
callback(event);
};

const destroy = clickUseCapture.subscribe(handleClick);

return {
destroy
};
};
1 change: 1 addition & 0 deletions packages/ui/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from './autofocus.js';
export * from './autofocusAfterTick.js';
export * from './autofocusWithDelay.js';
export * from './clickOutside.js';
export * from './focalPointToObjectPosition.js';
export * from './focusableSelector.js';
export * from './imageSizes.js';
Expand Down

0 comments on commit ef47ca8

Please sign in to comment.