-
Notifications
You must be signed in to change notification settings - Fork 0
/
formatchange.d.ts
111 lines (93 loc) · 4.01 KB
/
formatchange.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
type BaseMedia = { is: string; was?: string };
export declare type FormatMonitorMedia<Group extends string = string> =
string extends Group
? BaseMedia
: BaseMedia &
Record<`${"is" | "was" | "became" | "left"}${Group}`, boolean>;
// ---------------------------------------------------------------------------
export declare type FormatMonitorCallback<
T extends FormatMonitorMedia = FormatMonitorMedia
> = (media: T) => void;
// ---------------------------------------------------------------------------
export declare type FormatMonitorGroupConfig<Group extends string = string> =
string extends Group ? Record<never, never> : Record<Group, Record<string, boolean>>;
// ---------------------------------------------------------------------------
export declare type FormatMonitorOptions = {
/** Optionally supply a pre-existing element to query */
elm?: HTMLElement;
/** DOM id of the element to query. (Used if `elm` is missing)
*
* Default: `'mediaformat'`
*/
elmId?: string;
/** Tag-name used when auto-generating an element to query
*
* Default: `'del'`
*/
elmTagName?: string;
/** Set to `true` if you want to `.start()` manually */
defer?: boolean;
/** A custom `window` object/scope to monitor. */
win?: Window;
/** Set to `true` to disable `window.onresize` evend binding and run `.check()` manually */
manual?: false;
};
// ===========================================================================
export declare class FormatChange<
GroupConfig extends FormatMonitorGroupConfig,
Group extends string = GroupConfig extends FormatMonitorGroupConfig<infer G>
? G
: string
> {
constructor(groups?: GroupConfig, options?: FormatMonitorOptions);
/** The current FormatMonitorMedia object */
media: FormatMonitorMedia<Group>;
/**
* The groups object that was passed to the constructor.
* If this property is changed you must run `.refresh()`.
*/
formatGroups: object extends GroupConfig
? Record<never, never>
: GroupConfig extends undefined
? Record<never, never>
: GroupConfig;
/**
* Whenever FormatChange detects a new `format` it runs any callbacks that have `.subscribe()`d to be notified, passing them a reference to the `formatMonitor.media` object.
*
* Each callback is immediately run upon subscription if `formatMonitor.isRunning() === true` – so no separate "initialization" is required.
*
* If the callback should not be run immediately, then pass `false` as a second parameter to `.subscribe()` – like so: `formatMonitor.subscribe( myEventCallback, false )`
*/
subscribe(
callback: (media: FormatMonitorMedia<Group>) => void,
runImmediately?: boolean
): void;
/** Cancels a subscription */
unsubscribe(callback: (media: FormatMonitorMedia<Group>) => void): void;
/** Tells you if the `window.onresize` monitoring is active or not. If your monitor is set to `manual`, it simply tells you if it has been started. */
isRunning(): boolean;
/**
* Binds the `window.onresize` event handler to poll the CSS and trigger event callbacks.
* This method is called internally when a `FormatChange` instance is created – unless the `defer` option is passed.
*/
start(): void;
/**
* Stop monitoring.
* This does NOT unsubscribe any callbacks – only stops the onResize CSS-polling and triggering of events
*/
stop(): void;
/**
* Quickly queries if the format has changed and triggers subscriptions if needed. This is the method to use with the `manual` option.
*
* Returns `true` if a change was detected and subscriptions were triggered.
*/
check(): boolean;
/**
* Refreshes the `media` object and triggers subscriptions when appropriate – (always if a "hard-refresh" boolean argument is passed via `.refresh(true)`).
*
* This is the method to run if you have dynamically changed the formatGroups config or something weird.
*
* Returns `true` if the refresh was performed (i.e. if the monitoring `isRunning()`)
*/
refresh(hardRefresh?: boolean): boolean;
}