@gopherium/godmin 0.3.0 → 0.4.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
@@ -19,7 +19,7 @@ here duplicates something upstream ships.
19
19
 
20
20
  | Entry point | Contents |
21
21
  | --- | --- |
22
- | `@gopherium/godmin` | `AdminRoot`, `Frame`, `Page`, `PageTitle`, `NavScreen`, `ErrorNotice`, `LoadMore`, `LoadingScreen`, `LoadingRows`, `Toaster`, `useToaster`, `useMediaQuery`, `useTokenDocument`, the breakpoints, `SUPPORTED_WPDS` |
22
+ | `@gopherium/godmin` | `AdminRoot`, `Frame`, `Page`, `PageTitle`, `NavScreen`, `ErrorNotice`, `LoadMore`, `LoadingScreen`, `LoadingRows`, `useLoadingGate`, `Toaster`, `useToaster`, `useMediaQuery`, `useTokenDocument`, the breakpoints, `SUPPORTED_WPDS` |
23
23
  | `@gopherium/godmin/base.css` | cascade layer order, design tokens, host rules, frame and screen styles |
24
24
  | `@gopherium/godmin/router` | `useCanvas`, `useFrameLocation`, the `canvas` route static data |
25
25
  | `@gopherium/godmin/testing` | `installTestEnvironment`, `renderAdmin`, `setViewport`, `getAnnouncement`, `clearAnnouncements`, `assertElementPatched`, `WPDS_IGNORE_SELECTOR` |
@@ -145,15 +145,17 @@ label through a visually hidden status region, so give `label` the same
145
145
  sentence you would have rendered as text.
146
146
 
147
147
  ```tsx
148
- if (report.data === undefined) {
149
- return <LoadingScreen label="Loading the report." />
150
- }
148
+ const ghost = useLoadingGate(reports.isPending)
151
149
 
152
- {reports.isPending ? <LoadingRows label="Loading reports." rows={8} /> : <ReportTable />}
150
+ if (ghost) {
151
+ return <LoadingRows label="Loading reports." rows={8} />
152
+ }
153
153
  ```
154
154
 
155
- A ghost appears only after a short delay, so a load that resolves quickly
156
- shows nothing at all rather than a flash.
155
+ `useLoadingGate` owns the timing: a load that resolves quickly shows no
156
+ ghost at all, and a ghost that does appear stays long enough to read as
157
+ intentional. Gate the pending flag rather than using it directly, or a
158
+ mid speed load flashes the ghost for a moment.
157
159
 
158
160
  `base.css` also ships `godmin-empty`, `godmin-form`, `godmin-table` and
159
161
  `godmin-table-scroll`. The last two go together: a table wider than a phone
package/dist/base.css CHANGED
@@ -152,19 +152,6 @@ body {
152
152
  white-space: nowrap;
153
153
  }
154
154
 
155
- .godmin-loading-screen,
156
- .godmin-loading-rows {
157
- opacity: 0;
158
- animation: godmin-loading-appear var(--wpds-motion-duration-md)
159
- var(--wpds-motion-easing-balanced) 200ms forwards;
160
- }
161
-
162
- @keyframes godmin-loading-appear {
163
- 100% {
164
- opacity: 1;
165
- }
166
- }
167
-
168
155
  .godmin-loading-screen {
169
156
  padding: var(--wpds-dimension-padding-xl);
170
157
  }
package/dist/index.d.ts CHANGED
@@ -10,6 +10,7 @@ export { ErrorNotice, LoadMore, Page, PageTitle } from './page.js';
10
10
  export type { LoadMoreQuery, PageProps } from './page.js';
11
11
  export { Toaster, useToaster } from './toaster.js';
12
12
  export type { ToastAction, ToasterHandle, ToasterProps } from './toaster.js';
13
+ export { useLoadingGate } from './use-loading-gate.js';
13
14
  export { useMediaQuery } from './use-media-query.js';
14
15
  export { useTokenDocument } from './use-token-document.js';
15
16
  /**
package/dist/index.js CHANGED
@@ -6,6 +6,7 @@ export { LoadingRows, LoadingScreen } from './loading.js';
6
6
  export { NavScreen } from './nav-screen.js';
7
7
  export { ErrorNotice, LoadMore, Page, PageTitle } from './page.js';
8
8
  export { Toaster, useToaster } from './toaster.js';
9
+ export { useLoadingGate } from './use-loading-gate.js';
9
10
  export { useMediaQuery } from './use-media-query.js';
10
11
  export { useTokenDocument } from './use-token-document.js';
11
12
  /**
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Returns whether a loading ghost should stand in for the pending work.
3
+ * @param pending - Whether the work the ghost stands in for is still pending.
4
+ * @returns Whether to render the ghost.
5
+ */
6
+ export declare function useLoadingGate(pending: boolean): boolean;
@@ -0,0 +1,32 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ import { useEffect, useRef, useState } from 'react';
3
+ const SHOW_AFTER = 200;
4
+ const SHOW_FOR = 500;
5
+ /**
6
+ * Returns whether a loading ghost should stand in for the pending work.
7
+ * @param pending - Whether the work the ghost stands in for is still pending.
8
+ * @returns Whether to render the ghost.
9
+ */
10
+ export function useLoadingGate(pending) {
11
+ const [showing, setShowing] = useState(false);
12
+ const holdUntil = useRef(0);
13
+ useEffect(() => {
14
+ if (pending && !showing) {
15
+ const delay = setTimeout(() => {
16
+ holdUntil.current = Date.now() + SHOW_FOR;
17
+ setShowing(true);
18
+ }, SHOW_AFTER);
19
+ return () => clearTimeout(delay);
20
+ }
21
+ if (!pending && showing) {
22
+ const left = holdUntil.current - Date.now();
23
+ if (left <= 0) {
24
+ setShowing(false);
25
+ return;
26
+ }
27
+ const hold = setTimeout(() => setShowing(false), left);
28
+ return () => clearTimeout(hold);
29
+ }
30
+ }, [pending, showing]);
31
+ return showing;
32
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gopherium/godmin",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "The admin kit for Gopherium projects: host layer for React admin applications on the WordPress Design System.",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",