Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ function App() {

return (
<div>
<button onClick={handle.enter}>
Enter fullscreen
</button>
{handle.enabled &&
<button onClick={handle.enter}>
Enter fullscreen
</button>
}

<FullScreen handle={handle}>
Any fullscreen content here
Expand All @@ -42,6 +44,8 @@ function App() {
export default App;
```

`handle.enabled` indicates whether the browser supports fullscreen. Use it to conditionally render fullscreen controls.

When you have many elements you need one handle per element.
```jsx
import React, {useCallback} from 'react';
Expand Down Expand Up @@ -107,6 +111,10 @@ interface FullScreenHandle {
active: boolean;
// Specifies if attached element is currently full screen.

enabled: boolean;
// Specifies if the browser supports full screen.
// Use this to show/hide full screen button.

enter: () => Promise<void>;
// Requests this element to go full screen.

Expand Down
14 changes: 14 additions & 0 deletions src/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ describe('useFullScreenHandle', () => {
expect(hook.result.current.active).toBe(false);
});

describe('enabled flag', () => {
it('is true when fscreen.fullscreenEnabled is true', () => {
fscreen.fullscreenEnabled = true;
const { result } = renderHook(() => useFullScreenHandle());
expect(result.current.enabled).toBe(true);
});

it('is false when fscreen.fullscreenEnabled is false', () => {
fscreen.fullscreenEnabled = false;
const { result } = renderHook(() => useFullScreenHandle());
expect(result.current.enabled).toBe(false);
});
});

it('listens to fullscreen change', () => {
expect(fscreen.addEventListener).toHaveBeenCalledTimes(1);
expect(fscreen.addEventListener).toHaveBeenCalledWith(
Expand Down
3 changes: 3 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import fscreen from 'fscreen';

export interface FullScreenHandle {
active: boolean;
enabled: boolean;
enter: () => Promise<void>;
exit: () => Promise<void>;
node: React.MutableRefObject<HTMLDivElement | null>;
Expand All @@ -24,6 +25,7 @@ export interface FullScreenProps {
export function useFullScreenHandle(): FullScreenHandle {
const [active, setActive] = useState<boolean>(false);
const node = useRef<HTMLDivElement | null>(null);
const enabled = useMemo(() => fscreen.fullscreenEnabled, []);

useEffect(() => {
const handleChange = () => {
Expand Down Expand Up @@ -53,6 +55,7 @@ export function useFullScreenHandle(): FullScreenHandle {
return useMemo(
() => ({
active,
enabled,
enter,
exit,
node,
Expand Down