Skip to content

Commit 8733af8

Browse files
committedFeb 25, 2025·
fix(BottomSheet): close BottomSheet on orientation change
1 parent b563ea1 commit 8733af8

File tree

4 files changed

+68
-23
lines changed

4 files changed

+68
-23
lines changed
 

‎src/common/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const { default: useShell } = require('./useShell');
2424
const useStreamingServer = require('./useStreamingServer');
2525
const useTorrent = require('./useTorrent');
2626
const useTranslate = require('./useTranslate');
27-
const useWindowSize = require('./useWindowSize');
27+
const { default: useOrientation } = require('./useOrientation');
2828

2929
module.exports = {
3030
FileDropProvider,
@@ -56,5 +56,5 @@ module.exports = {
5656
useStreamingServer,
5757
useTorrent,
5858
useTranslate,
59-
useWindowSize,
59+
useOrientation,
6060
};

‎src/common/useOrientation.ts

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright (C) 2017-2025 Smart code 203358507
2+
3+
import { useState, useEffect } from 'react';
4+
5+
type DeviceOrientationData = {
6+
alpha: number | null;
7+
beta: number | null;
8+
gamma: number | null;
9+
absolute: boolean | null;
10+
permissionGranted: boolean;
11+
};
12+
13+
const useOrientation = () => {
14+
const [orientation, setOrientation] = useState<DeviceOrientationData>({
15+
alpha: null,
16+
beta: null,
17+
gamma: null,
18+
absolute: null,
19+
permissionGranted: false,
20+
});
21+
22+
const requestPermission = async () => {
23+
if (
24+
typeof DeviceOrientationEvent !== 'undefined' &&
25+
(DeviceOrientationEvent as any).requestPermission
26+
) {
27+
try {
28+
const permissionState = await (DeviceOrientationEvent as any).requestPermission();
29+
if (permissionState === 'granted') {
30+
setOrientation((prev) => ({ ...prev, permissionGranted: true }));
31+
}
32+
} catch (error) {
33+
console.error('Error requesting DeviceOrientation permission:', error);
34+
}
35+
} else {
36+
setOrientation((prev) => ({ ...prev, permissionGranted: true }));
37+
}
38+
};
39+
40+
useEffect(() => {
41+
const handleOrientationChange = (event: DeviceOrientationEvent) => {
42+
setOrientation((prev) => ({
43+
...prev,
44+
alpha: event.alpha ?? null,
45+
beta: event.beta ?? null,
46+
gamma: event.gamma ?? null,
47+
absolute: event.absolute ?? null,
48+
}));
49+
};
50+
51+
if (orientation.permissionGranted && window.DeviceOrientationEvent) {
52+
window.addEventListener('deviceorientation', handleOrientationChange);
53+
}
54+
55+
return () => {
56+
window.removeEventListener('deviceorientation', handleOrientationChange);
57+
};
58+
}, [orientation.permissionGranted]);
59+
60+
return { ...orientation, requestPermission };
61+
};
62+
63+
export default useOrientation;

‎src/common/useWindowSize.js

-18
This file was deleted.

‎src/components/BottomSheet/BottomSheet.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'
44
import { createPortal } from 'react-dom';
55
import classNames from 'classnames';
66
import useBinaryState from 'stremio/common/useBinaryState';
7-
import useWindowSize from 'stremio/common/useWindowSize';
7+
import useOrientation from 'stremio/common/useOrientation';
88
import styles from './BottomSheet.less';
99

1010
const CLOSE_THRESHOLD = 100;
@@ -18,7 +18,7 @@ type Props = {
1818

1919
const BottomSheet = ({ children, title, show, onClose }: Props) => {
2020
const containerRef = useRef<HTMLDivElement>(null);
21-
const { width: windowWidth, height: windowHeight } = useWindowSize();
21+
const orientation = useOrientation();
2222
const [startOffset, setStartOffset] = useState(0);
2323
const [offset, setOffset] = useState(0);
2424

@@ -62,7 +62,7 @@ const BottomSheet = ({ children, title, show, onClose }: Props) => {
6262

6363
useEffect(() => {
6464
opened && close();
65-
}, [windowWidth, windowHeight]);
65+
}, [orientation]);
6666

6767
return opened && createPortal((
6868
<div className={styles['bottom-sheet']}>

0 commit comments

Comments
 (0)
Please sign in to comment.