mirror of
https://github.com/darksaid98/oxmysql.git
synced 2026-12-02 06:28:54 +00:00
50 lines
1.5 KiB
TypeScript
50 lines
1.5 KiB
TypeScript
import React, { Context, createContext, useContext, useEffect, useState } from 'react';
|
|
import { useNuiEvent } from '../hooks/useNuiEvent';
|
|
import { fetchNui } from '../utils/fetchNui';
|
|
|
|
const VisibilityCtx = createContext<VisibilityProviderValue | null>(null);
|
|
|
|
interface VisibilityProviderValue {
|
|
setVisible: (visible: boolean) => void;
|
|
visible: boolean;
|
|
}
|
|
|
|
// This should be mounted at the top level of your application, it is currently set to
|
|
// apply a CSS visibility value. If this is non-performant, this should be customized.
|
|
export const VisibilityProvider: React.FC = ({ children }) => {
|
|
const [visible, setVisible] = useState(false);
|
|
|
|
useNuiEvent<boolean>('openUI', setVisible);
|
|
|
|
// Handle pressing escape/backspace
|
|
useEffect(() => {
|
|
// Only attach listener when we are visible
|
|
if (!visible) return;
|
|
|
|
const keyHandler = (e: KeyboardEvent) => {
|
|
if (['Escape'].includes(e.code)) {
|
|
fetchNui('exit');
|
|
setVisible(false);
|
|
}
|
|
};
|
|
|
|
window.addEventListener('keydown', keyHandler);
|
|
|
|
return () => window.removeEventListener('keydown', keyHandler);
|
|
}, [visible]);
|
|
|
|
return (
|
|
<VisibilityCtx.Provider
|
|
value={{
|
|
visible,
|
|
setVisible,
|
|
}}
|
|
>
|
|
<div style={{ visibility: visible ? 'visible' : 'hidden', height: '100%' }}>{children}</div>
|
|
</VisibilityCtx.Provider>
|
|
);
|
|
};
|
|
|
|
export const useVisibility = () =>
|
|
useContext<VisibilityProviderValue>(VisibilityCtx as Context<VisibilityProviderValue>);
|