|
1 | | -export interface ViewportFunctions { |
2 | | - /** |
3 | | - * A method that runs when the viewport is first entered (triggered only once). |
4 | | - */ |
5 | | - oninit?(): void |
6 | | - /** |
7 | | - * A method to fire when you enter the viewport. |
| 1 | +interface ViewportFunctions { |
| 2 | + /** |
| 3 | + * A method that runs when the viewport is first entered (triggered only once). |
| 4 | + */ |
| 5 | + oninit?(): void; |
| 6 | + /** |
| 7 | + * A method to fire when you enter the viewport. |
| 8 | + */ |
| 9 | + onenter?(): void; |
| 10 | + /** |
| 11 | + * A method to fire when you leave the viewport. |
| 12 | + */ |
| 13 | + onexit?(): void; |
| 14 | + /** |
| 15 | + * A method to fire upon screen resize |
| 16 | + */ |
| 17 | + onresize?(screenX?: number): void; |
| 18 | +} |
| 19 | +interface ViewportOptions extends ViewportFunctions { |
| 20 | + /** |
| 21 | + * An ID reference for the viewport. This will be used in queries |
| 22 | + */ |
| 23 | + id: string; |
| 24 | + /** |
| 25 | + * The media query to match |
| 26 | + * |
| 27 | + * @example |
| 28 | + * |
| 29 | + * '(min-width: 768px)' |
| 30 | + */ |
| 31 | + query: string; |
| 32 | +} |
| 33 | +interface ViewportScreen { |
| 34 | + /** |
| 35 | + * Whether or not viewport is active |
| 36 | + */ |
| 37 | + active: boolean; |
| 38 | + /** |
| 39 | + * The ID reference for the viewport |
| 40 | + */ |
| 41 | + id: string; |
| 42 | + /** |
| 43 | + * The media query being matches |
| 44 | + */ |
| 45 | + query: string; |
| 46 | + /** |
| 47 | + * Match media instances |
| 48 | + */ |
| 49 | + test: MediaQueryList; |
| 50 | + /** |
| 51 | + * A `Set` List of methods that will be triggered upon initializing within the viewport |
| 52 | + */ |
| 53 | + oninit: Set<() => void>; |
| 54 | + /** |
| 55 | + * A `Set` List of methods that will be triggered when entering the viewport |
8 | 56 | */ |
9 | | - onenter?(): void |
10 | | - /** |
11 | | - * A method to fire when you leave the viewport. |
| 57 | + onenter: Set<() => void>; |
| 58 | + /** |
| 59 | + * A `Set` List of methods that will be triggered when existing the viewport |
12 | 60 | */ |
13 | | - onexit?(): void |
14 | | - /** |
15 | | - * A method to fire upon screen resize |
| 61 | + onexit: Set<() => void>; |
| 62 | + /** |
| 63 | + * A `Set` List of methods that will be triggered when resizing the screen. |
16 | 64 | */ |
17 | | - onresize?(screenX?: number): void |
| 65 | + onresize: Set<(screenX?: number) => void>; |
18 | 66 | } |
| 67 | +/** |
| 68 | + * The Viewports store |
| 69 | + * |
| 70 | + * Holds a reference to all states |
| 71 | + */ |
| 72 | +declare const viewports: Map<string, ReturnType<typeof create>>; |
| 73 | +/** |
| 74 | + * Create |
| 75 | + * |
| 76 | + * Create viewport states |
| 77 | + */ |
| 78 | +declare function create(opts: ViewportOptions): { |
| 79 | + onenter: () => void; |
| 80 | + onexit: () => void; |
| 81 | + onresize: (x: number) => void; |
| 82 | + destroy: () => void; |
| 83 | + readonly screen: ViewportScreen; |
| 84 | +}; |
| 85 | +/** |
| 86 | + * Create Screens |
| 87 | + * |
| 88 | + * Define a set of viewports to match against. You can |
| 89 | + * optionally include methods or use `add` to set methods |
| 90 | + * at a later time. |
| 91 | + */ |
| 92 | +declare const screens: (options: ViewportOptions | ViewportOptions[]) => void; |
| 93 | +/** |
| 94 | + * Get Viewport |
| 95 | + * |
| 96 | + * Returns a viewport instance by its `id` else `false` |
| 97 | + */ |
| 98 | +declare const get: (id: string) => false | { |
| 99 | + onenter: () => void; |
| 100 | + onexit: () => void; |
| 101 | + onresize: (x: number) => void; |
| 102 | + destroy: () => void; |
| 103 | + readonly screen: ViewportScreen; |
| 104 | +}; |
| 105 | +/** |
| 106 | + * Add Methods |
| 107 | + * |
| 108 | + * Add methods to be invoked in when the viewport is matched. |
| 109 | + */ |
| 110 | +declare const add: (id: string, actions: ViewportFunctions) => void; |
| 111 | +/** |
| 112 | + * List Screens |
| 113 | + * |
| 114 | + * Returns a list of viewport screens from the store. Optionally |
| 115 | + * provide a `string[]` list of ids. |
| 116 | + */ |
| 117 | +declare const list: (ids?: string[]) => ViewportScreen[]; |
| 118 | +/** |
| 119 | + * Check State |
| 120 | + * |
| 121 | + * Returns a `boolean` value informing on whether the provided |
| 122 | + * viewport id is active or not. When no `id` parameter is passed |
| 123 | + * a list of viewport screens which are active will be returned. |
| 124 | + */ |
| 125 | +declare const active: (id?: string) => boolean | ViewportScreen | ViewportScreen[]; |
| 126 | +/** |
| 127 | + * Remove viewport query |
| 128 | + * |
| 129 | + * Removes and destroys the viewport screen and store with |
| 130 | + * the matching `id` provided. |
| 131 | + */ |
| 132 | +declare const remove: (id: string) => void; |
| 133 | +/** |
| 134 | + * Destroy |
| 135 | + * |
| 136 | + * Removes all instances and tears down the listeners. |
| 137 | + */ |
| 138 | +declare const destroy: () => void; |
19 | 139 |
|
20 | | -export interface ViewportOptions extends ViewportFunctions { |
21 | | - /** |
22 | | - * An ID reference for the viewport. This will be used in queries |
23 | | - */ |
24 | | - id: string; |
25 | | - /** |
26 | | - * The media query to match |
27 | | - * |
28 | | - * @example |
29 | | - * |
30 | | - * '(min-width: 768px)' |
31 | | - */ |
32 | | - query: string; |
33 | | -} |
34 | | - |
35 | | -export interface ViewportScreen { |
36 | | - /** |
37 | | - * Whether or not viewport is active |
38 | | - */ |
39 | | - active: boolean; |
40 | | - /** |
41 | | - * The ID reference for the viewport |
42 | | - */ |
43 | | - id: string |
44 | | - /** |
45 | | - * The media query being matches |
46 | | - */ |
47 | | - query: string; |
48 | | - /** |
49 | | - * Match media instances |
50 | | - */ |
51 | | - test: MediaQueryList; |
52 | | - /** |
53 | | - * A `Set` List of methods that will be triggered upon initializing within the viewport |
54 | | - */ |
55 | | - oninit: Set<() => void>; |
56 | | - /** |
57 | | - * A `Set` List of methods that will be triggered when entering the viewport |
58 | | - */ |
59 | | - onenter: Set<() => void>; |
60 | | - /** |
61 | | - * A `Set` List of methods that will be triggered when existing the viewport |
62 | | - */ |
63 | | - onexit: Set<() => void>; |
64 | | - /** |
65 | | - * A `Set` List of methods that will be triggered when resizing the screen. |
66 | | - */ |
67 | | - onresize: Set<(screenX?: number) => void>; |
68 | | -} |
| 140 | +export { active, add, destroy, get, list, remove, screens, viewports }; |
0 commit comments