@indietabletop/appkit 0.2.1 → 0.2.3

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.
@@ -0,0 +1,6 @@
1
+ import { AsyncOp } from "./async-op.js";
2
+ export declare function useAsyncOp<T, E>(): {
3
+ op: AsyncOp<T, E>;
4
+ setSuccess: (value: T) => void;
5
+ setFailure: (failure: E) => void;
6
+ };
@@ -0,0 +1,12 @@
1
+ import { useCallback, useState } from "react";
2
+ import { Failure, Pending, Success } from "./async-op.js";
3
+ export function useAsyncOp() {
4
+ const [op, setOp] = useState(new Pending());
5
+ const setSuccess = useCallback((value) => {
6
+ setOp(new Success(value));
7
+ }, []);
8
+ const setFailure = useCallback((failure) => {
9
+ setOp(new Failure(failure));
10
+ }, []);
11
+ return { op, setSuccess, setFailure };
12
+ }
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Sets document background color, reverting it to previous color on unmount.
3
+ */
4
+ export declare function useDocumentBackgroundColor(bodyColor: string): void;
@@ -0,0 +1,14 @@
1
+ import { useEffect } from "react";
2
+ /**
3
+ * Sets document background color, reverting it to previous color on unmount.
4
+ */
5
+ export function useDocumentBackgroundColor(bodyColor) {
6
+ useEffect(() => {
7
+ const style = window.document.documentElement.style;
8
+ const originalColor = style.backgroundColor;
9
+ style.backgroundColor = bodyColor;
10
+ return () => {
11
+ style.backgroundColor = originalColor;
12
+ };
13
+ });
14
+ }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Checks whether the app is installed.
3
+ *
4
+ * Note that this doesn't check whether the app is installed on the device at
5
+ * all, only whether the currently running process is within an installed window
6
+ * or running within a browser.
7
+ */
8
+ export declare function useIsInstalled(): boolean;
@@ -0,0 +1,13 @@
1
+ import { useMemo } from "react";
2
+ /**
3
+ * Checks whether the app is installed.
4
+ *
5
+ * Note that this doesn't check whether the app is installed on the device at
6
+ * all, only whether the currently running process is within an installed window
7
+ * or running within a browser.
8
+ */
9
+ export function useIsInstalled() {
10
+ // The only way to get into the Standalone display mode is to install
11
+ // the app, so this is a good way to check installation status.
12
+ return useMemo(() => window.matchMedia("(display-mode: standalone)").matches, []);
13
+ }
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Sets a state that will automatically revert to null after specified number
3
+ * of milliseconds.
4
+ */
5
+ export declare function useRevertingState<T>(initialState: T, revertAfterMs: number): readonly [T | null, import("react").Dispatch<import("react").SetStateAction<T | null>>];
@@ -0,0 +1,26 @@
1
+ import { useEffect, useRef, useState } from "react";
2
+ /**
3
+ * Sets a state that will automatically revert to null after specified number
4
+ * of milliseconds.
5
+ */
6
+ export function useRevertingState(initialState, revertAfterMs) {
7
+ const [state, setState] = useState(initialState);
8
+ const timeoutRef = useRef(null);
9
+ useEffect(() => {
10
+ const timeoutId = timeoutRef.current;
11
+ if (timeoutId) {
12
+ clearInterval(timeoutId);
13
+ }
14
+ if (state) {
15
+ timeoutRef.current = setTimeout(() => {
16
+ setState(null);
17
+ }, revertAfterMs);
18
+ }
19
+ return () => {
20
+ if (timeoutId) {
21
+ clearTimeout(timeoutId);
22
+ }
23
+ };
24
+ }, [revertAfterMs, state]);
25
+ return [state, setState];
26
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@indietabletop/appkit",
3
- "version": "0.2.1",
3
+ "version": "0.2.3",
4
4
  "description": "A collection of modules used in apps built by Indie Tabletop Club",
5
5
  "private": false,
6
6
  "type": "module",