@maz-ui/mcp 5.0.0-beta.17 → 5.0.0-beta.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/mcp.d.mts +22 -9
- package/dist/mcp.d.ts +22 -9
- package/dist/mcp.mjs +160 -83
- package/docs/src/blog/v4.md +1 -1
- package/docs/src/blog/v5.md +4 -4
- package/docs/src/components/maz-btn.md +1 -1
- package/docs/src/components/maz-date-picker.md +1 -1
- package/docs/src/components/maz-icon.md +2 -2
- package/docs/src/directives/click-outside.md +1 -1
- package/docs/src/directives/fullscreen-img.md +1 -1
- package/docs/src/directives/lazy-img.md +1 -1
- package/docs/src/directives/tooltip.md +1 -1
- package/docs/src/directives/zoom-img.md +1 -1
- package/docs/src/{guide/icons.md → ecosystem/icons/index.md} +1 -1
- package/docs/src/ecosystem/node/exec-promise.md +87 -0
- package/docs/src/ecosystem/node/index.md +53 -0
- package/docs/src/ecosystem/node/logger.md +146 -0
- package/docs/src/ecosystem/node/print-banner.md +93 -0
- package/docs/src/{guide → ecosystem}/nuxt.md +1 -1
- package/docs/src/{guide → ecosystem}/themes.md +4 -4
- package/docs/src/{guide → ecosystem}/translations.md +1 -1
- package/docs/src/ecosystem/utils/camel-case.md +31 -0
- package/docs/src/ecosystem/utils/check-availability.md +79 -0
- package/docs/src/ecosystem/utils/cookie.md +80 -0
- package/docs/src/ecosystem/utils/debounce-callback.md +38 -0
- package/docs/src/ecosystem/utils/debounce-id.md +69 -0
- package/docs/src/ecosystem/utils/debounce.md +65 -0
- package/docs/src/ecosystem/utils/fetch-locale-ip.md +33 -0
- package/docs/src/ecosystem/utils/format-json.md +33 -0
- package/docs/src/ecosystem/utils/format-phone-number.md +37 -0
- package/docs/src/ecosystem/utils/get-browser-locale.md +29 -0
- package/docs/src/ecosystem/utils/get-error-message.md +39 -0
- package/docs/src/ecosystem/utils/idle-timeout.md +90 -0
- package/docs/src/ecosystem/utils/index.md +60 -0
- package/docs/src/ecosystem/utils/is-client.md +32 -0
- package/docs/src/ecosystem/utils/is-equal.md +38 -0
- package/docs/src/ecosystem/utils/is-server.md +31 -0
- package/docs/src/ecosystem/utils/is-standalone-mode.md +43 -0
- package/docs/src/ecosystem/utils/kebab-case.md +36 -0
- package/docs/src/ecosystem/utils/normalize-string.md +77 -0
- package/docs/src/ecosystem/utils/pascal-case.md +35 -0
- package/docs/src/ecosystem/utils/script-loader.md +77 -0
- package/docs/src/ecosystem/utils/sleep.md +59 -0
- package/docs/src/ecosystem/utils/snake-case.md +36 -0
- package/docs/src/ecosystem/utils/swipe-handler.md +91 -0
- package/docs/src/ecosystem/utils/textarea-autogrow.md +41 -0
- package/docs/src/ecosystem/utils/throttle-id.md +48 -0
- package/docs/src/ecosystem/utils/throttle.md +57 -0
- package/docs/src/ecosystem/utils/truthy-filter.md +31 -0
- package/docs/src/ecosystem/utils/types/deep-key-of.md +48 -0
- package/docs/src/ecosystem/utils/types/deep-partial.md +42 -0
- package/docs/src/ecosystem/utils/types/deep-required.md +39 -0
- package/docs/src/ecosystem/utils/types/flatten-object-keys.md +44 -0
- package/docs/src/ecosystem/utils/types/generic-instance-type.md +42 -0
- package/docs/src/ecosystem/utils/types/infer-maybe-ref.md +35 -0
- package/docs/src/ecosystem/utils/upper-first.md +32 -0
- package/docs/src/ecosystem/utils/user-visibility.md +69 -0
- package/docs/src/guide/getting-started.md +2 -2
- package/docs/src/guide/migration-v4.md +6 -6
- package/docs/src/guide/resolvers.md +7 -7
- package/docs/src/guide/vue.md +4 -4
- package/docs/src/index.md +12 -12
- package/docs/src/plugins/aos.md +1 -1
- package/package.json +5 -5
- package/docs/src/helpers/camel-case.md +0 -14
- package/docs/src/helpers/check-availability.md +0 -14
- package/docs/src/helpers/debounce-callback.md +0 -14
- package/docs/src/helpers/debounce-id.md +0 -14
- package/docs/src/helpers/debounce.md +0 -14
- package/docs/src/helpers/is-client.md +0 -14
- package/docs/src/helpers/is-equal.md +0 -14
- package/docs/src/helpers/is-standalone-mode.md +0 -14
- package/docs/src/helpers/kebab-case.md +0 -14
- package/docs/src/helpers/normalize-string.md +0 -14
- package/docs/src/helpers/pascal-case.md +0 -14
- package/docs/src/helpers/script-loader.md +0 -14
- package/docs/src/helpers/sleep.md +0 -14
- package/docs/src/helpers/snake-case.md +0 -14
- package/docs/src/helpers/throttle-id.md +0 -14
- package/docs/src/helpers/throttle.md +0 -14
- /package/docs/src/{guide → ecosystem/icons}/icon-set.md +0 -0
- /package/docs/src/{guide → ecosystem}/mcp.md +0 -0
- /package/docs/src/{helpers → ecosystem/utils}/capitalize.md +0 -0
- /package/docs/src/{helpers → ecosystem/utils}/country-code-to-unicode-flag.md +0 -0
- /package/docs/src/{helpers → ecosystem/utils}/currency.md +0 -0
- /package/docs/src/{helpers → ecosystem/utils}/date.md +0 -0
- /package/docs/src/{helpers → ecosystem/utils}/get-country-flag-url.md +0 -0
- /package/docs/src/{helpers → ecosystem/utils}/number.md +0 -0
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: debounceCallback
|
|
3
|
+
description: Module-scoped one-shot debounce — schedule a callback that gets reset on each new call.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# {{ $frontmatter.title }}
|
|
7
|
+
|
|
8
|
+
{{ $frontmatter.description }}
|
|
9
|
+
|
|
10
|
+
Unlike [`debounce`](./debounce), which builds a debounced wrapper around a function, `debounceCallback` is a procedural helper: each call schedules a callback after `delay` ms, cancelling any previous pending callback.
|
|
11
|
+
|
|
12
|
+
::: warning Shared module state
|
|
13
|
+
`debounceCallback` uses a single module-level timer. All consumers of this function share the same timer slot, so calling it from two unrelated places will cancel each other. Use [`debounceId`](./debounce-id) for isolated debounces.
|
|
14
|
+
:::
|
|
15
|
+
|
|
16
|
+
## Usage
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import { debounceCallback } from '@maz-ui/utils'
|
|
20
|
+
|
|
21
|
+
debounceCallback(() => {
|
|
22
|
+
console.log('runs only if no other call happens in the next 300 ms')
|
|
23
|
+
}, 300)
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## API
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
function debounceCallback(
|
|
30
|
+
callback: (...args: unknown[]) => unknown,
|
|
31
|
+
delay: number,
|
|
32
|
+
): void
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
| Parameter | Type | Description |
|
|
36
|
+
| ---------- | ---------- | ------------------------------------------ |
|
|
37
|
+
| `callback` | `Function` | Function to run after the quiet window |
|
|
38
|
+
| `delay` | `number` | Quiet window before firing, in milliseconds |
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: debounceId
|
|
3
|
+
description: Debounce an async function with isolated state per identifier — only the last call resolves, others are cancelled.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# {{ $frontmatter.title }}
|
|
7
|
+
|
|
8
|
+
{{ $frontmatter.description }}
|
|
9
|
+
|
|
10
|
+
`debounceId` is the right tool when:
|
|
11
|
+
|
|
12
|
+
- You need to debounce **async** work and `await` the result.
|
|
13
|
+
- You want **per-key isolation** — debounces for `'search'` shouldn't interfere with debounces for `'autosave'`.
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { debounceId } from '@maz-ui/utils'
|
|
19
|
+
|
|
20
|
+
const searchUsers = debounceId(
|
|
21
|
+
'user-search',
|
|
22
|
+
async (query: string) => {
|
|
23
|
+
const res = await fetch(`/api/users?q=${query}`)
|
|
24
|
+
return res.json()
|
|
25
|
+
},
|
|
26
|
+
300,
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
const users = await searchUsers('alice')
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## API
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
function debounceId<T, Args extends unknown[]>(
|
|
36
|
+
identifier: string,
|
|
37
|
+
func: (...args: Args) => T | Promise<T>,
|
|
38
|
+
delay: number,
|
|
39
|
+
): (...args: Args) => Promise<T>
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
| Parameter | Type | Description |
|
|
43
|
+
| ------------ | --------------- | ------------------------------------------------------------ |
|
|
44
|
+
| `identifier` | `string` | Unique key — calls with the same key share a debounce timer |
|
|
45
|
+
| `func` | `Function` | The async (or sync) function to debounce |
|
|
46
|
+
| `delay` | `number` | Quiet window before the call resolves, in ms |
|
|
47
|
+
|
|
48
|
+
Returns a function that always returns a `Promise<T>`. Earlier pending promises are cancelled — only the last invocation resolves.
|
|
49
|
+
|
|
50
|
+
## Examples
|
|
51
|
+
|
|
52
|
+
Autosave a form, keyed by document ID, so concurrent edits to different documents don't share state:
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
import { debounceId } from '@maz-ui/utils'
|
|
56
|
+
|
|
57
|
+
async function saveDocument(id: string, payload: unknown) {
|
|
58
|
+
const save = debounceId(
|
|
59
|
+
`doc-${id}`,
|
|
60
|
+
(body: unknown) => fetch(`/docs/${id}`, { method: 'PUT', body: JSON.stringify(body) }),
|
|
61
|
+
500,
|
|
62
|
+
)
|
|
63
|
+
return save(payload)
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Related
|
|
68
|
+
|
|
69
|
+
- [`throttleId`](./throttle-id) — same shape, but throttles instead of debounces.
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: debounce
|
|
3
|
+
description: Wrap a function so it only runs after a quiet period — every new call resets the timer.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# {{ $frontmatter.title }}
|
|
7
|
+
|
|
8
|
+
{{ $frontmatter.description }}
|
|
9
|
+
|
|
10
|
+
Useful for input handlers, resize listeners, search-as-you-type and any callback that should not fire on every event.
|
|
11
|
+
|
|
12
|
+
## Usage
|
|
13
|
+
|
|
14
|
+
```ts
|
|
15
|
+
import { debounce } from '@maz-ui/utils'
|
|
16
|
+
|
|
17
|
+
const onSearch = debounce((query: string) => {
|
|
18
|
+
console.log('searching for', query)
|
|
19
|
+
}, 300)
|
|
20
|
+
|
|
21
|
+
onSearch('h')
|
|
22
|
+
onSearch('he')
|
|
23
|
+
onSearch('hel')
|
|
24
|
+
// only the last call (with "hel") runs, 300 ms after the third call
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## API
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
function debounce<F extends (...args: any[]) => any>(
|
|
31
|
+
fn: F,
|
|
32
|
+
delay: number,
|
|
33
|
+
): (...args: Parameters<F>) => void
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
| Parameter | Type | Description |
|
|
37
|
+
| --------- | ---------- | --------------------------------------------- |
|
|
38
|
+
| `fn` | `Function` | The function to debounce |
|
|
39
|
+
| `delay` | `number` | Quiet window in milliseconds before `fn` runs |
|
|
40
|
+
|
|
41
|
+
The returned function shares no state with other debounced wrappers — each call to `debounce()` creates a fresh timer.
|
|
42
|
+
|
|
43
|
+
## Examples
|
|
44
|
+
|
|
45
|
+
Debounce a Vue input handler:
|
|
46
|
+
|
|
47
|
+
```vue
|
|
48
|
+
<script setup lang="ts">
|
|
49
|
+
import { debounce } from '@maz-ui/utils'
|
|
50
|
+
|
|
51
|
+
const onInput = debounce((event: Event) => {
|
|
52
|
+
console.log((event.target as HTMLInputElement).value)
|
|
53
|
+
}, 250)
|
|
54
|
+
</script>
|
|
55
|
+
|
|
56
|
+
<template>
|
|
57
|
+
<input @input="onInput">
|
|
58
|
+
</template>
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Related
|
|
62
|
+
|
|
63
|
+
- [`debounceId`](./debounce-id) — debounce an async function, keyed by an identifier, returning a promise.
|
|
64
|
+
- [`debounceCallback`](./debounce-callback) — module-scoped one-shot debounce.
|
|
65
|
+
- [`throttle`](./throttle) — rate-limits instead of waiting for silence.
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: fetchLocaleIp
|
|
3
|
+
description: Resolve the visitor's ISO country code from their IP address using the free `ipwho.is` service.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# {{ $frontmatter.title }}
|
|
7
|
+
|
|
8
|
+
{{ $frontmatter.description }}
|
|
9
|
+
|
|
10
|
+
Useful for pre-filling locale-aware UI — phone-number country selectors, currency hints, default language — before the user has interacted with the page.
|
|
11
|
+
|
|
12
|
+
## Usage
|
|
13
|
+
|
|
14
|
+
```ts
|
|
15
|
+
import { fetchLocaleIp } from '@maz-ui/utils'
|
|
16
|
+
|
|
17
|
+
const countryCode = await fetchLocaleIp()
|
|
18
|
+
// 'FR' | 'US' | … or undefined if the request fails
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## API
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
function fetchLocaleIp(): Promise<string | undefined>
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Returns the ISO 3166-1 alpha-2 country code (e.g. `'FR'`), or `undefined` if the request fails. Errors are caught and logged via `console.error` — the call never throws.
|
|
28
|
+
|
|
29
|
+
## Notes
|
|
30
|
+
|
|
31
|
+
- Hits `https://ipwho.is` — a public, no-key service. Consider rate limits and privacy for production usage.
|
|
32
|
+
- Returns `undefined` rather than throwing so a fallback path (default locale, manual selection) can be wired without `try/catch`.
|
|
33
|
+
- For a static locale source, see [`getBrowserLocale`](./get-browser-locale).
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: formatJson
|
|
3
|
+
description: Tiny wrapper around `JSON.stringify` with sensible default indentation.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# {{ $frontmatter.title }}
|
|
7
|
+
|
|
8
|
+
{{ $frontmatter.description }}
|
|
9
|
+
|
|
10
|
+
## Usage
|
|
11
|
+
|
|
12
|
+
```ts
|
|
13
|
+
import { formatJson } from '@maz-ui/utils'
|
|
14
|
+
|
|
15
|
+
formatJson({ name: 'Alice', age: 30 })
|
|
16
|
+
// {
|
|
17
|
+
// "name": "Alice",
|
|
18
|
+
// "age": 30
|
|
19
|
+
// }
|
|
20
|
+
|
|
21
|
+
formatJson({ a: 1 }, 4) // → indent with 4 spaces
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## API
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
function formatJson(json: unknown, indent?: number): string
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
| Parameter | Type | Default | Description |
|
|
31
|
+
| --------- | --------- | ------- | ---------------------------------------- |
|
|
32
|
+
| `json` | `unknown` | — | Any JSON-serialisable value |
|
|
33
|
+
| `indent` | `number` | `2` | Number of spaces per indentation level |
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: formatPhoneNumber
|
|
3
|
+
description: Format an arbitrary phone-number string into its international representation using `libphonenumber-js`.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# {{ $frontmatter.title }}
|
|
7
|
+
|
|
8
|
+
{{ $frontmatter.description }}
|
|
9
|
+
|
|
10
|
+
If the input can't be parsed, the original string is returned unchanged.
|
|
11
|
+
|
|
12
|
+
## Usage
|
|
13
|
+
|
|
14
|
+
```ts
|
|
15
|
+
import { formatPhoneNumber } from '@maz-ui/utils'
|
|
16
|
+
|
|
17
|
+
formatPhoneNumber('+33612345678') // → '+33 6 12 34 56 78'
|
|
18
|
+
formatPhoneNumber('0612345678') // → '0612345678' (unparseable without country context — returned as-is)
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## API
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
function formatPhoneNumber(phoneNumber: string): string
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
| Parameter | Type | Description |
|
|
28
|
+
| ------------- | -------- | ---------------------------------------- |
|
|
29
|
+
| `phoneNumber` | `string` | The phone number to format (E.164 ideal) |
|
|
30
|
+
|
|
31
|
+
Throws a `TypeError` when `phoneNumber` is empty.
|
|
32
|
+
|
|
33
|
+
## Notes
|
|
34
|
+
|
|
35
|
+
- For maximum portability, supply the input in E.164 (`+CCXXXXXXXXX`). Otherwise, parsing falls back to passing the input through unchanged.
|
|
36
|
+
- For interactive phone-number input with country selection, see the [`MazInputPhoneNumber`](/components/maz-input-phone-number) component.
|
|
37
|
+
- Built on [`libphonenumber-js`](https://github.com/catamphetamine/libphonenumber-js).
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: getBrowserLocale
|
|
3
|
+
description: Read the user's browser language tag (e.g. `'fr-FR'`, `'en-US'`) — returns `undefined` on the server.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# {{ $frontmatter.title }}
|
|
7
|
+
|
|
8
|
+
{{ $frontmatter.description }}
|
|
9
|
+
|
|
10
|
+
## Usage
|
|
11
|
+
|
|
12
|
+
```ts
|
|
13
|
+
import { getBrowserLocale } from '@maz-ui/utils'
|
|
14
|
+
|
|
15
|
+
const locale = getBrowserLocale() // 'fr-FR' | 'en-US' | … | undefined
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## API
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
function getBrowserLocale(): string | undefined
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Returns `navigator.language` on the client. On the server (no `navigator`), returns `undefined`.
|
|
25
|
+
|
|
26
|
+
## Notes
|
|
27
|
+
|
|
28
|
+
- For IP-based country detection instead of the browser-declared locale, see [`fetchLocaleIp`](./fetch-locale-ip).
|
|
29
|
+
- The returned tag follows [BCP 47](https://www.rfc-editor.org/info/bcp47).
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: getErrorMessage
|
|
3
|
+
description: Extract a human-readable message from anything thrown — `Error`, `string`, `{ message }` objects, or arbitrary values.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# {{ $frontmatter.title }}
|
|
7
|
+
|
|
8
|
+
{{ $frontmatter.description }}
|
|
9
|
+
|
|
10
|
+
Especially useful inside `catch` blocks where the caught value is typed `unknown`.
|
|
11
|
+
|
|
12
|
+
## Usage
|
|
13
|
+
|
|
14
|
+
```ts
|
|
15
|
+
import { getErrorMessage } from '@maz-ui/utils'
|
|
16
|
+
|
|
17
|
+
try {
|
|
18
|
+
await doSomething()
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
21
|
+
console.error(getErrorMessage(error))
|
|
22
|
+
}
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## API
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
function getErrorMessage(error: unknown): string
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Resolution rules
|
|
32
|
+
|
|
33
|
+
The helper inspects `error` in this order:
|
|
34
|
+
|
|
35
|
+
1. `error instanceof Error` → returns `error.message`.
|
|
36
|
+
2. `typeof error === 'string'` → returns `error` as-is.
|
|
37
|
+
3. `error` is a plain object with a `message` property → returns `String(error.message)`.
|
|
38
|
+
4. `error` is otherwise truthy → returns `String(error)`.
|
|
39
|
+
5. `error` is falsy → returns `'An unexpected error occurred'`.
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: IdleTimeout
|
|
3
|
+
description: Detect user inactivity (mouse, keyboard, touch, scroll) and run a callback when the user becomes idle.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# {{ $frontmatter.title }}
|
|
7
|
+
|
|
8
|
+
{{ $frontmatter.description }}
|
|
9
|
+
|
|
10
|
+
`IdleTimeout` listens for a range of user-input events (`mousedown`, `keydown`, `touchstart`, `wheel`, …) on a target element. When none of them fire for `timeout` ms, the user is considered idle and your callback is invoked.
|
|
11
|
+
|
|
12
|
+
## Usage
|
|
13
|
+
|
|
14
|
+
```ts
|
|
15
|
+
import { IdleTimeout } from '@maz-ui/utils'
|
|
16
|
+
|
|
17
|
+
const watcher = new IdleTimeout(
|
|
18
|
+
({ isIdle }) => {
|
|
19
|
+
if (isIdle) {
|
|
20
|
+
console.log('User went idle')
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
console.log('User is back')
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
{ timeout: 60_000 }, // 1 minute
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
// later
|
|
30
|
+
watcher.destroy()
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## API
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
class IdleTimeout {
|
|
37
|
+
constructor(callback: IdleTimeoutCallback, options?: IdleTimeoutOptions)
|
|
38
|
+
|
|
39
|
+
start(): void
|
|
40
|
+
pause(): void
|
|
41
|
+
resume(): void
|
|
42
|
+
reset(): void
|
|
43
|
+
destroy(): void
|
|
44
|
+
|
|
45
|
+
readonly destroyed: boolean
|
|
46
|
+
idle: boolean
|
|
47
|
+
timeout: number
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
type IdleTimeoutCallback = (payload: {
|
|
51
|
+
isIdle: boolean
|
|
52
|
+
eventType?: string
|
|
53
|
+
instance: IdleTimeout
|
|
54
|
+
}) => unknown
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Options
|
|
58
|
+
|
|
59
|
+
| Option | Type | Default | Description |
|
|
60
|
+
| ----------- | -------------------------- | ----------- | -------------------------------------------------------------------------------------- |
|
|
61
|
+
| `timeout` | `number` | `300_000` | Idle duration in milliseconds (default 5 minutes). |
|
|
62
|
+
| `element` | `HTMLElement \| Document` | `document.body` | Element to attach event listeners to. |
|
|
63
|
+
| `once` | `boolean` | `false` | When `true`, automatically calls `destroy()` after the first idle trigger. |
|
|
64
|
+
| `immediate` | `boolean` | `true` | Fire the callback once on construction. Useful to set `false` in SSR contexts. |
|
|
65
|
+
|
|
66
|
+
### Methods
|
|
67
|
+
|
|
68
|
+
- **`start()`** — Attach listeners and (re)start the timer. The constructor calls this automatically on the client.
|
|
69
|
+
- **`pause()` / `resume()`** — Freeze and unfreeze the countdown without losing the elapsed time.
|
|
70
|
+
- **`reset()`** — Restart the timer from scratch.
|
|
71
|
+
- **`destroy()`** — Detach all listeners. The watcher cannot be reused after destroy.
|
|
72
|
+
|
|
73
|
+
## Examples
|
|
74
|
+
|
|
75
|
+
Auto-logout after 10 minutes of inactivity:
|
|
76
|
+
|
|
77
|
+
```ts
|
|
78
|
+
const idle = new IdleTimeout(
|
|
79
|
+
({ isIdle }) => {
|
|
80
|
+
if (isIdle) {
|
|
81
|
+
logout()
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
{ timeout: 10 * 60 * 1000, once: true },
|
|
85
|
+
)
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## Related
|
|
89
|
+
|
|
90
|
+
- [`UserVisibility`](./user-visibility) — track tab focus / visibility instead of in-page activity.
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: '@maz-ui/utils'
|
|
3
|
+
description: Lightweight, tree-shakeable utility functions and TypeScript helpers for Vue, Nuxt and any JavaScript/TypeScript project.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# {{ $frontmatter.title }}
|
|
7
|
+
|
|
8
|
+
{{ $frontmatter.description }}
|
|
9
|
+
|
|
10
|
+
<NpmBadge package="@maz-ui/utils" />
|
|
11
|
+
|
|
12
|
+
`@maz-ui/utils` ships the runtime helpers and TypeScript type utilities that power maz-ui internally, packaged as a standalone, framework-agnostic library. No Vue dependency, no global side effects — just import what you need.
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
::: code-group
|
|
17
|
+
|
|
18
|
+
```bash [pnpm]
|
|
19
|
+
pnpm add @maz-ui/utils
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
```bash [npm]
|
|
23
|
+
npm install @maz-ui/utils
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
```bash [yarn]
|
|
27
|
+
yarn add @maz-ui/utils
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
:::
|
|
31
|
+
|
|
32
|
+
## Basic usage
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
import { debounce, formatCurrency, sleep } from '@maz-ui/utils'
|
|
36
|
+
|
|
37
|
+
const debounced = debounce(() => console.log('typed'), 300)
|
|
38
|
+
|
|
39
|
+
const price = formatCurrency(1999.9, 'fr-FR', { currency: 'EUR' })
|
|
40
|
+
|
|
41
|
+
await sleep(500)
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## What's inside
|
|
45
|
+
|
|
46
|
+
<UtilsCatalogue />
|
|
47
|
+
|
|
48
|
+
## Tree-shaking
|
|
49
|
+
|
|
50
|
+
The package is published with `"sideEffects": false`. Named imports — `import { sleep } from '@maz-ui/utils'` — are statically analysed by Vite, Rollup, esbuild and webpack: any helper you don't reference is dropped from your bundle.
|
|
51
|
+
|
|
52
|
+
For granular bundling you can also import a single helper through its dedicated subpath:
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
import { sleep } from '@maz-ui/utils/helpers/sleep'
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## TypeScript helpers
|
|
59
|
+
|
|
60
|
+
Beyond runtime utilities, `@maz-ui/utils` also exports a set of generic type helpers (`DeepPartial`, `DeepKeyOf`, `FlattenObjectKeys`, …) under `@maz-ui/utils/ts-helpers`. See the **TypeScript Helpers** section in the sidebar.
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: isClient
|
|
3
|
+
description: Check whether the code is running in a browser environment (has a `document`).
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# {{ $frontmatter.title }}
|
|
7
|
+
|
|
8
|
+
{{ $frontmatter.description }}
|
|
9
|
+
|
|
10
|
+
## Usage
|
|
11
|
+
|
|
12
|
+
```ts
|
|
13
|
+
import { isClient } from '@maz-ui/utils'
|
|
14
|
+
|
|
15
|
+
if (isClient()) {
|
|
16
|
+
document.body.classList.add('hydrated')
|
|
17
|
+
}
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## API
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
function isClient(): boolean
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Returns `true` if `document` is defined, `false` otherwise (e.g. SSR, Node.js, Web Workers).
|
|
27
|
+
|
|
28
|
+
## Notes
|
|
29
|
+
|
|
30
|
+
- The opposite is [`isServer`](./is-server).
|
|
31
|
+
- Implemented as a one-liner: `typeof document !== 'undefined'`.
|
|
32
|
+
- Prefer this over checking `process.client` (Nuxt) or `import.meta.env.SSR` (Vite) when you want a framework-agnostic check.
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: isEqual
|
|
3
|
+
description: Deep structural equality check for primitives, arrays, plain objects and Dates.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# {{ $frontmatter.title }}
|
|
7
|
+
|
|
8
|
+
{{ $frontmatter.description }}
|
|
9
|
+
|
|
10
|
+
## Usage
|
|
11
|
+
|
|
12
|
+
```ts
|
|
13
|
+
import { isEqual } from '@maz-ui/utils'
|
|
14
|
+
|
|
15
|
+
isEqual({ a: 1, b: [2, 3] }, { a: 1, b: [2, 3] }) // true
|
|
16
|
+
isEqual([1, 2, 3], [1, 2, 3]) // true
|
|
17
|
+
isEqual(new Date('2024-01-01'), new Date('2024-01-01')) // true
|
|
18
|
+
isEqual({ a: 1 }, { a: 1, b: 2 }) // false
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## API
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
function isEqual(a: unknown, b: unknown): boolean
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
| Parameter | Type | Description |
|
|
28
|
+
| --------- | --------- | ----------------- |
|
|
29
|
+
| `a` | `unknown` | First value |
|
|
30
|
+
| `b` | `unknown` | Second value |
|
|
31
|
+
|
|
32
|
+
## Behaviour
|
|
33
|
+
|
|
34
|
+
- **Primitives** (`null`, `undefined`, `string`, `number`, `boolean`, `symbol`, `bigint`) — compared with `===`.
|
|
35
|
+
- **Dates** — compared by `getTime()`.
|
|
36
|
+
- **Arrays** — same length and every element deeply equal.
|
|
37
|
+
- **Plain objects** — same set of keys and every value deeply equal.
|
|
38
|
+
- **Anything else** (Maps, Sets, class instances, functions) is not specifically handled; comparisons fall back to `===`.
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: isServer
|
|
3
|
+
description: Check whether the code is running on the server (no `window` or `document`).
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# {{ $frontmatter.title }}
|
|
7
|
+
|
|
8
|
+
{{ $frontmatter.description }}
|
|
9
|
+
|
|
10
|
+
## Usage
|
|
11
|
+
|
|
12
|
+
```ts
|
|
13
|
+
import { isServer } from '@maz-ui/utils'
|
|
14
|
+
|
|
15
|
+
if (isServer()) {
|
|
16
|
+
return placeholder
|
|
17
|
+
}
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## API
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
function isServer(): boolean
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Returns `true` when either `document` or `window` is undefined — covers Node.js, Bun, Deno, Edge runtimes and Web Workers.
|
|
27
|
+
|
|
28
|
+
## Notes
|
|
29
|
+
|
|
30
|
+
- The opposite is [`isClient`](./is-client).
|
|
31
|
+
- Useful for guarding `window`-dependent code from SSR errors.
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: isStandaloneMode
|
|
3
|
+
description: Check whether the app is running as an installed Progressive Web App (PWA) rather than inside a browser tab.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# {{ $frontmatter.title }}
|
|
7
|
+
|
|
8
|
+
{{ $frontmatter.description }}
|
|
9
|
+
|
|
10
|
+
## Usage
|
|
11
|
+
|
|
12
|
+
```ts
|
|
13
|
+
import { isStandaloneMode } from '@maz-ui/utils'
|
|
14
|
+
|
|
15
|
+
if (isStandaloneMode()) {
|
|
16
|
+
console.log('Running as installed PWA')
|
|
17
|
+
}
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## API
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
function isStandaloneMode(): boolean
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Returns `true` if the page is rendered in standalone display mode, `false` otherwise (including during SSR).
|
|
27
|
+
|
|
28
|
+
## Detection rules
|
|
29
|
+
|
|
30
|
+
The check returns `true` when **either**:
|
|
31
|
+
|
|
32
|
+
- `window.matchMedia('(display-mode: standalone)').matches` — covers Chrome, Edge, Firefox, etc.
|
|
33
|
+
- `navigator.standalone` is truthy — iOS Safari's legacy property.
|
|
34
|
+
|
|
35
|
+
## Examples
|
|
36
|
+
|
|
37
|
+
Hide install prompts when already installed:
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
if (!isStandaloneMode()) {
|
|
41
|
+
showInstallBanner()
|
|
42
|
+
}
|
|
43
|
+
```
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: kebabCase
|
|
3
|
+
description: Convert any common string casing to kebab-case — handles camelCase, PascalCase, snake_case, spaces and consecutive capitals.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# {{ $frontmatter.title }}
|
|
7
|
+
|
|
8
|
+
{{ $frontmatter.description }}
|
|
9
|
+
|
|
10
|
+
## Usage
|
|
11
|
+
|
|
12
|
+
```ts
|
|
13
|
+
import { kebabCase } from '@maz-ui/utils'
|
|
14
|
+
|
|
15
|
+
kebabCase('myComponent') // → 'my-component'
|
|
16
|
+
kebabCase('MyComponent') // → 'my-component'
|
|
17
|
+
kebabCase('XMLParser') // → 'xml-parser'
|
|
18
|
+
kebabCase('hello_world') // → 'hello-world'
|
|
19
|
+
kebabCase('hello world') // → 'hello-world'
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## API
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
function kebabCase(str: string): string
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
| Parameter | Type | Description |
|
|
29
|
+
| --------- | -------- | --------------------- |
|
|
30
|
+
| `str` | `string` | The string to convert |
|
|
31
|
+
|
|
32
|
+
## Notes
|
|
33
|
+
|
|
34
|
+
- Consecutive uppercase letters are handled (e.g. `XMLParser` → `xml-parser`).
|
|
35
|
+
- Leading and trailing dashes are stripped.
|
|
36
|
+
- Multiple separators collapse into a single dash.
|