Skip to content

Commit 83e0ebf

Browse files
author
Quang Phan
committed
feat(clickoutside): ambient types for extended attributes
1 parent 7e7a452 commit 83e0ebf

6 files changed

Lines changed: 82 additions & 37 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@svelte-put/clickoutside": patch
3+
---
4+
5+
provide automatic ambient types for extended attributes

apps/svelte-kit/src/app.d.ts

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,7 @@ declare namespace App {
1515
}
1616

1717
declare namespace svelte.JSX {
18-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
19-
type EventHandler<E = Event, T = HTMLElement> = (event: E & { target: EventTarget & T }) => any;
20-
21-
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unused-vars
22-
type CustomEventHandler<T, D = any> = EventHandler<CustomEvent<D>, T>;
23-
24-
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unused-vars
25-
type HTMLAttrs<T> = {
26-
[key in keyof EventList as `on${key}`]?: CustomEventHandler<T, EventList[key]>;
27-
};
28-
29-
// eslint-disable-next-line @typescript-eslint/no-empty-interface, @typescript-eslint/no-unused-vars
30-
interface HTMLAttributes<T> {
31-
// on:clickoutside
32-
onclickoutside?: (event: CustomEvent<MouseEvent>) => void;
18+
export interface HTMLAttributes {
3319
// on:movablestart
3420
onmovablestart?: (
3521
event: CustomEvent<import('@svelte-put/movable').MovableEventDetails>,
@@ -44,5 +30,3 @@ declare namespace svelte.JSX {
4430
ontoc?: (event: CustomEvent<import('@svelte-put/toc').TocEventDetails>) => void;
4531
}
4632
}
47-
48-
declare module '*.svelte';

packages/actions/clickoutside/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,27 @@ See [examples here](https://github.com/vnphanquang/svelte-put/blob/main/packages
5656

5757
### Typescript support
5858

59+
Ambient types for `on:clickoutside` should be available automatically when `clickoutside` is imported
60+
61+
<details open>
62+
<summary> show / hide </summary>
63+
64+
```html
65+
<script lang="ts">
66+
import { clickoutside } from '@svelte-put/clickoutside';
67+
</script>
68+
69+
<!-- on:clickoutside should be typed -->
70+
<div
71+
use:clickoutside
72+
on:clickoutside
73+
/>
74+
```
75+
76+
</details>
77+
78+
If the above is not working, fall back to this:
79+
5980
<details open>
6081
<summary> app.d.ts: show / hide </summary>
6182

packages/actions/clickoutside/src/clickoutside.ts

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,12 @@
1-
/**
2-
* Limit to which the click event will trigger `clickoutside`
3-
* @public
4-
*
5-
* @remarks
6-
* Currently only the parent option is supported
7-
*/
8-
export interface ClickOutsideLimit {
9-
/** Click event beyond the `boundingRect` of this parent node will not trigger `clickoutside` */
10-
parent: HTMLElement;
11-
}
1+
import { ClickOutsideAttributes, ClickOutsideParameters } from './clickoutside.types';
122

13-
/**
14-
* svelte action parameters to config behavior of `clickoutside`
15-
* @public
16-
*/
17-
export interface ClickOutsideParameters {
18-
/** whether to activate the action. Default to `true` */
19-
enabled: boolean;
20-
/** limit to which the click event will trigger `clickoutside` */
21-
limit?: ClickOutsideLimit;
3+
// ambient typing
4+
declare global {
5+
// eslint-disable-next-line @typescript-eslint/no-namespace
6+
export namespace svelte.JSX {
7+
// eslint-disable-next-line @typescript-eslint/no-empty-interface
8+
export interface HTMLAttributes extends ClickOutsideAttributes {}
9+
}
2210
}
2311

2412
/**
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Additional attributes extended from `svelte-put/clickoutside`
3+
* @public
4+
*
5+
* @remarks
6+
* The ambient types for these extended attributes should be available automatically
7+
* whenever `svelte-put/clickoutside` is imported.
8+
*
9+
* ```html
10+
* <script lang="ts">
11+
* import { clickoutside } from '@svelte-put/clickoutside';
12+
* </script>
13+
*
14+
* <!-- on:clickoutside should be typed -->
15+
* <div
16+
* use:clickoutside
17+
* on:clickoutside
18+
* />
19+
* ```
20+
*/
21+
export interface ClickOutsideAttributes {
22+
onclickoutside?: (event: CustomEvent<MouseEvent>) => void; // on:clickoutside
23+
}
24+
25+
/**
26+
* Limit to which the click event will trigger `clickoutside`
27+
* @public
28+
*
29+
* @remarks
30+
* Currently only the parent option is supported
31+
*/
32+
export interface ClickOutsideLimit {
33+
/** Click event beyond the `boundingRect` of this parent node will not trigger `clickoutside` */
34+
parent: HTMLElement;
35+
}
36+
37+
/**
38+
* svelte action parameters to config behavior of `clickoutside`
39+
* @public
40+
*/
41+
export interface ClickOutsideParameters {
42+
/** whether to activate the action. Default to `true` */
43+
enabled: boolean;
44+
/** limit to which the click event will trigger `clickoutside` */
45+
limit?: ClickOutsideLimit;
46+
}

packages/actions/clickoutside/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
*/
88

99
export * from './clickoutside';
10+
export * from './clickoutside.types';

0 commit comments

Comments
 (0)