Skip to content

Commit

Permalink
add onclickoutside
Browse files Browse the repository at this point in the history
  • Loading branch information
swyx committed Nov 1, 2020
1 parent 2906ef5 commit 1600d59
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -8,3 +8,5 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).

## 0.0.1

hello world
35 changes: 30 additions & 5 deletions README.md
Expand Up @@ -10,7 +10,29 @@ do not rely on this library yet!

### `clickOutside`

to be completed
`export function clickOutside(node: HTMLElement, params: {enabled: boolean, cb: Function }): ReturnType<Action>`

Call callback when user clicks outside a given element.

Demo: https://svelte.dev/repl/dae848c2157e48ab932106779960f5d5?version=3.19.2


```svelte
<script>
import {clickOutside} from 'svelte-actions'
let open = true;
</script>
<div use:clickOutside={{ enabled: open, cb: () => open = false }}>
<button on:click={() => open = true}>Open</button>
{#if open}
<span>
Opened
</span>
{/if}
</div>
```

### `longpress`

Expand All @@ -22,10 +44,13 @@ Demo: https://svelte.dev/tutorial/adding-parameters-to-actions

```svelte
<script>
import {lazyLoad} from 'svelte-actions'
import {longpress} from 'svelte-actions'
</script>
<img use:lazyLoad={{src:"/myimage"}} alt="">
<button use:longpress={duration}
on:longpress="{() => pressed = true}"
on:mouseenter="{() => pressed = false}"
>press and hold</button>
```

### `pannable`
Expand All @@ -34,7 +59,7 @@ Demo: https://svelte.dev/tutorial/adding-parameters-to-actions

Creates `panStart`, `panMove`, `panEnd` events so you can drag elements. Demo: https://svelte.dev/tutorial/actions

### `lazyLoad`
### `lazyload`

`export function lazyLoad(node: HTMLElement, attributes: Object): ReturnType<Action>`

Expand All @@ -44,7 +69,7 @@ Demo: https://svelte.dev/repl/f12988de576b4bf9b541a2a59eb838f6?version=3.23.2

```svelte
<script>
import {lazyLoad} from 'svelte-actions'
import {lazyload} from 'svelte-actions'
</script>
<img use:lazyLoad={{src:"/myimage"}} alt="">
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -6,6 +6,7 @@
"types": "dist/index.d.ts",
"scripts": {
"build": "tsc",
"preversion": "npm run build",
"version": "auto-changelog -p --template keepachangelog && git add CHANGELOG.md",
"prepublishOnly": "git push && git push --tags && gh-release"
},
Expand Down
37 changes: 33 additions & 4 deletions src/index.ts
Expand Up @@ -3,10 +3,39 @@ type Action = (node: HTMLElement, parameters: any) => {
destroy?: () => void
}

/**
*
* Call callback when user clicks outside a given element
*
* Usage:
* <div use:clickOutside={{ enabled: open, cb: () => open = false }}>
*
* Demo: https://svelte.dev/repl/dae848c2157e48ab932106779960f5d5?version=3.19.2
*
*/
export function clickOutside(node: HTMLElement, params: {enabled: boolean, cb: Function }): ReturnType<Action> {
const { enabled: initialEnabled, cb } = params

const handleOutsideClick = ({ target }: MouseEvent) => {
if (!node.contains(target as Node)) cb(); // typescript hack, not sure how to solve without asserting as Node
};

function update({enabled}: {enabled: boolean}) {
if (enabled) {
window.addEventListener('click', handleOutsideClick);
} else {
window.removeEventListener('click', handleOutsideClick);
}
}
update({ enabled: initialEnabled });
return {
update,
destroy() {
window.removeEventListener( 'click', handleOutsideClick );
}
};

// export function clickOutside(): ReturnType<Action> {

// }
}


/**
Expand Down Expand Up @@ -113,7 +142,7 @@ export function pannable(node: HTMLElement): ReturnType<Action> {
* Demo: https://svelte.dev/repl/f12988de576b4bf9b541a2a59eb838f6?version=3.23.2
*
*/
export function lazyLoad(node: HTMLElement, attributes: Object): ReturnType<Action> {
export function lazyload(node: HTMLElement, attributes: Object): ReturnType<Action> {
let intersecting = false;

const handleIntersection: IntersectionObserverCallback = (entries) => {
Expand Down

0 comments on commit 1600d59

Please sign in to comment.