@vibehooks/react 0.0.6 → 0.0.7

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.
@@ -16,28 +16,37 @@ import * as React from "react";
16
16
  * @see https://developer.mozilla.org/en-US/docs/Web/API/Screen_Orientation_API
17
17
  */
18
18
  function useScreenOrientation() {
19
- const isSupported = typeof screen !== "undefined" && screen.orientation !== void 0;
20
- const [type, setType] = React.useState(isSupported ? screen.orientation.type : null);
21
- const [angle, setAngle] = React.useState(isSupported ? screen.orientation.angle : null);
22
- const lock = React.useCallback(async (orientation) => {
23
- if (!isSupported) return;
24
- await screen.orientation.lock(orientation);
25
- }, [isSupported]);
19
+ const orientation = typeof window !== "undefined" && "orientation" in screen ? screen.orientation : null;
20
+ const isSupported = !!orientation && typeof orientation.lock === "function" && typeof orientation.unlock === "function";
21
+ const [type, setType] = React.useState(orientation?.type ?? null);
22
+ const [angle, setAngle] = React.useState(orientation?.angle ?? null);
23
+ const lock = React.useCallback(async (orientationLock) => {
24
+ if (!isSupported || !orientation) return;
25
+ try {
26
+ await orientation.lock(orientationLock);
27
+ } catch (error) {
28
+ console.warn("Orientation lock failed:", error);
29
+ }
30
+ }, [isSupported, orientation]);
26
31
  const unlock = React.useCallback(() => {
27
- if (!isSupported) return;
28
- screen.orientation.unlock();
29
- }, [isSupported]);
32
+ if (!isSupported || !orientation) return;
33
+ try {
34
+ orientation.unlock();
35
+ } catch (error) {
36
+ console.warn("Orientation unlock failed:", error);
37
+ }
38
+ }, [isSupported, orientation]);
30
39
  React.useEffect(() => {
31
- if (!isSupported) return;
40
+ if (!orientation) return;
32
41
  const handleChange = () => {
33
- setType(screen.orientation.type);
34
- setAngle(screen.orientation.angle);
42
+ setType(orientation.type);
43
+ setAngle(orientation.angle);
35
44
  };
36
- screen.orientation.addEventListener("change", handleChange);
45
+ orientation.addEventListener("change", handleChange);
37
46
  return () => {
38
- screen.orientation.removeEventListener("change", handleChange);
47
+ orientation.removeEventListener("change", handleChange);
39
48
  };
40
- }, [isSupported]);
49
+ }, [orientation]);
41
50
  return {
42
51
  angle,
43
52
  isSupported,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vibehooks/react",
3
3
  "type": "module",
4
- "version": "0.0.6",
4
+ "version": "0.0.7",
5
5
  "description": "Modern React and Next.js hooks, unopinionated and focused on developer experience.",
6
6
  "author": "Sebastian Marat Urdanegui Bisalaya <sebasurdanegui@gmail.com>",
7
7
  "license": "MIT",