Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add enabled prop #39

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
42 changes: 38 additions & 4 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ export interface UserInactivityProps<T = unknown> {
*/
children: React.ReactNode;

/**
* When set to false, cancels running timers
* When set to true, resets timer
*/
isEnabled?: boolean;

/**
* If set to true, the timer is not reset when the keyboard appears
* or disappears.
Expand All @@ -77,6 +83,7 @@ const UserInactivity: React.FC<UserInactivityProps> = ({
isActive,
onAction,
skipKeyboard,
isEnabled,
style,
timeForInactivity,
timeoutHandler,
Expand Down Expand Up @@ -107,6 +114,21 @@ const UserInactivity: React.FC<UserInactivityProps> = ({
}
}, [isActive]);

/**
* When ths isEnabled prop is changed to true, set the timer.
* When the pro is set to false, cancel the timer.
*/
const initialEnabled = isEnabled === undefined ? true : isEnabled;
const enabled = useRef(initialEnabled);
useEffect(() => {
enabled.current = isEnabled === undefined ? true : isEnabled;
if (isEnabled) {
setTimer();
} else {
cancelTimer();
}
}, [isEnabled]);

const [date, setDate] = useState(Date.now());

/**
Expand All @@ -127,6 +149,10 @@ const UserInactivity: React.FC<UserInactivityProps> = ({
useEffect(() => {
if (isFirstRender.current) {
isFirstRender.current = false;
// on the first render, cancel the initial timer if the component's isEnabled is false
if (!initialEnabled) {
cancelTimer();
}
} else {
if (active) {
onAction(true);
Expand Down Expand Up @@ -157,14 +183,22 @@ const UserInactivity: React.FC<UserInactivityProps> = ({
* This method is called whenever a touch is detected. If no touch is
* detected after `this.props.timeForInactivity` milliseconds, then
* `this.state.inactive` turns to true.
* isEnabled set to false will supress resetting the timer
*/
function resetTimerDueToActivity() {
if (!enabled.current) {
return;
}
cancelTimer();
setActive(true);

/**
* Causes `useTimeout` to restart.
*/
setTimer();
}

/**
* Causes `useTimeout` to restart.
*/
function setTimer() {
setActive(true);
setDate(Date.now());
}

Expand Down