@xeplr/ui-utils 1.0.12 → 1.1.0

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/README.md CHANGED
@@ -171,6 +171,23 @@ export default function SelectCompany() {
171
171
  | `STATES`, `STATES_IN` | `{ IN: [{ code, name }] }`, `[{ code, name }]` | 36 Indian states and union territories |
172
172
  | `FILE_UPLOAD_RULES`, `IMPORTS_RULES`, `DATA_IMPORT_RULES`, `IMPORT_HISTORY_RULES`, `DROPPED_ROWS_RULES`, `NUMBER_FIELD_RULES`, `DATE_FIELD_RULES`, `DATE_RANGE_RULES`, `RANGE_FIELD_RULES`, `DROPDOWN_RULES`, `GRID_DISPLAYER_RULES`, `TEXT_FIELD_RULES`, `COLOR_PICKER_RULES`, `EXPANDERS_RULES`, `SETTINGS_TREE_RULES` | rule arrays | What each page requires of a design |
173
173
 
174
+ ## Where you are lives in the URL: `useUrlParams`, `useUrlState`
175
+
176
+ The suite's rule: every screen state a person navigates to (an open editor or drawer, the selected record, the active tab or step, a dialog that is a place) is read from and written to the URL's query, so a refresh or a shared link lands on exactly the same screen. Transient things (hover, a half-typed form, a toast) are not.
177
+
178
+ ```js
179
+ import { useUrlParams, useUrlState } from '@xeplr/ui-utils/src/url/useUrlState.js'
180
+
181
+ const [tab, setTab] = useUrlState('tab', { default: 'canvas', replace: true }) // switching: no history entry
182
+ const [drawer, setDrawer] = useUrlState('drawer') // opening/closing: Back closes it
183
+ const url = useUrlParams({ tab: 'canvas' })
184
+ url.set({ widget: id, drawer: null }) // several at once
185
+ ```
186
+
187
+ - **Opening or closing** something adds a history entry; **switching** a tab or step, or typing, replaces it (`replace: true`).
188
+ - `null`, `''`, `false` and a value equal to its default are removed from the URL; `true` is written as `1`.
189
+ - Imported from `src/url/useUrlState.js`, not the package root, because it needs `react-router-dom` (an optional peer) and a consumer without a router shouldn't have to install one. The pure rule is `src/url/urlState.js` (`patchSearch`, `readSearch`), tested in `test/urlState.test.js`.
190
+
174
191
  ## DropdownPage
175
192
 
176
193
  | prop | type | default | meaning |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xeplr/ui-utils",
3
- "version": "1.0.12",
3
+ "version": "1.1.0",
4
4
  "description": "UI utility components: file upload with progress, snackbar, static data (countries/states)",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -27,9 +27,15 @@
27
27
  },
28
28
  "peerDependencies": {
29
29
  "react": "^18.0.0 || ^19.0.0",
30
- "@xeplr/ui-table": "^1.0.0"
30
+ "@xeplr/ui-table": "^1.0.0",
31
+ "react-router-dom": "^6.0.0 || ^7.0.0"
31
32
  },
32
33
  "scripts": {
33
34
  "test": "for f in test/*.test.js; do node \"$f\" || exit 1; done"
35
+ },
36
+ "peerDependenciesMeta": {
37
+ "react-router-dom": {
38
+ "optional": true
39
+ }
34
40
  }
35
41
  }
@@ -0,0 +1,33 @@
1
+ // WHERE YOU ARE LIVES IN THE URL — the pure half.
2
+ //
3
+ // Every screen state a person navigates to (an open editor or drawer, the
4
+ // selected record, the active tab or step, a dialog that is a place) is read
5
+ // from and written to the URL's query, so a refresh or a shared link lands on
6
+ // exactly the same screen. This file is the rule, with no React: how a patch
7
+ // of values becomes the next query string.
8
+ //
9
+ // null / undefined / '' → the key is removed (the default needs no URL)
10
+ // true → '1'
11
+ // anything else → String(value)
12
+ // a value equal to `defaults[key]` → removed too, so URLs stay short
13
+
14
+ export function patchSearch(search, patch, defaults) {
15
+ const next = new URLSearchParams(search || '')
16
+ Object.keys(patch || {}).forEach((k) => {
17
+ let v = patch[k]
18
+ if (v === true) v = '1'
19
+ if (v === false || v === null || v === undefined || v === '' ||
20
+ (defaults && defaults[k] !== undefined && String(defaults[k]) === String(v))) {
21
+ next.delete(k)
22
+ } else {
23
+ next.set(k, String(v))
24
+ }
25
+ })
26
+ return next
27
+ }
28
+
29
+ /** A query value read back: the string, or the default, or null. */
30
+ export function readSearch(params, key, fallback) {
31
+ const v = params.get(key)
32
+ return v === null || v === '' ? (fallback === undefined ? null : fallback) : v
33
+ }
@@ -0,0 +1,49 @@
1
+ import { useCallback } from 'react'
2
+ import { useSearchParams } from 'react-router-dom'
3
+ import { patchSearch, readSearch } from './urlState.js'
4
+
5
+ /**
6
+ * Read and write screen state in the URL's query (see urlState.js).
7
+ *
8
+ * const url = useUrlParams({ tab: 'canvas' })
9
+ * url.get('tab') → 'canvas' when absent
10
+ * url.set({ drawer: 'filters' }) → opens: a new history entry
11
+ * url.set({ tab: 'model' }, { replace: true }) → switching: no entry
12
+ *
13
+ * The rule the suite follows: OPENING or CLOSING something adds a history
14
+ * entry, so Back closes it; switching a tab or step, or typing, replaces it,
15
+ * so Back is not a keystroke undo.
16
+ *
17
+ * Needs react-router-dom (a peer of whatever app uses it). Imported from
18
+ * '@xeplr/ui-utils/src/url/useUrlState.js' rather than the package root, so a
19
+ * consumer without a router is not made to install one.
20
+ */
21
+ export function useUrlParams(defaults) {
22
+ const [params, setParams] = useSearchParams()
23
+ const get = useCallback((key) => readSearch(params, key, defaults ? defaults[key] : undefined), [params, defaults])
24
+ const set = useCallback((patch, opts) => {
25
+ setParams((prev) => patchSearch(prev, patch, defaults), { replace: Boolean(opts && opts.replace) })
26
+ }, [setParams, defaults])
27
+ return { get: get, set: set, params: params }
28
+ }
29
+
30
+ /**
31
+ * One value in the URL, as a [value, setValue] pair — useState's shape.
32
+ * `opts.replace` makes every change replace the history entry (a tab); the
33
+ * default adds one (an open panel). `opts.default` is what absence means.
34
+ */
35
+ export function useUrlState(key, opts) {
36
+ const o = opts || {}
37
+ const [params, setParams] = useSearchParams()
38
+ const value = readSearch(params, key, o.default)
39
+ const setValue = useCallback((v, callOpts) => {
40
+ const replace = callOpts && callOpts.replace !== undefined ? callOpts.replace : Boolean(o.replace)
41
+ setParams((prev) => {
42
+ const current = readSearch(prev, key, o.default)
43
+ const next = typeof v === 'function' ? v(current) : v
44
+ const defaults = o.default !== undefined ? { [key]: o.default } : undefined
45
+ return patchSearch(prev, { [key]: next }, defaults)
46
+ }, { replace: replace })
47
+ }, [setParams, key, o.default, o.replace])
48
+ return [value, setValue]
49
+ }