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

feat: Add temporary focus to enabled option in workspace initial page #38546

Merged
merged 23 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
31 changes: 30 additions & 1 deletion src/components/MenuItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ type MenuItemBaseProps = {
/** Whether item is focused or active */
focused?: boolean;

/** Temporarily focus the item indicating an action (like setting enable) occured */
shouldFocusTemporarily?: boolean;

/** The duration to focus the item temporarily */
temporaryFocusDuration?: number;

/** Should we disable this menu item? */
disabled?: boolean;

Expand Down Expand Up @@ -276,6 +282,8 @@ function MenuItem(
success = false,
focused = false,
disabled = false,
shouldFocusTemporarily = false,
temporaryFocusDuration = 3000,
title,
subtitle,
shouldShowBasicTitle,
Expand Down Expand Up @@ -315,7 +323,9 @@ function MenuItem(
const {isSmallScreenWidth} = useWindowDimensions();
const [html, setHtml] = useState('');
const titleRef = useRef('');
const tempFocusTimeoutRef = useRef<NodeJS.Timeout>();
luacmartins marked this conversation as resolved.
Show resolved Hide resolved
const {isExecuting, singleExecution, waitForNavigate} = useContext(MenuItemGroupContext) ?? {};
const [temporaryilyFocused, setTemporaryilyFocused] = useState(false);
luacmartins marked this conversation as resolved.
Show resolved Hide resolved

const isDeleted = style && Array.isArray(style) ? style.includes(styles.offlineFeedback.deleted) : false;
const descriptionVerticalMargin = shouldShowDescriptionOnTop ? styles.mb1 : styles.mt1;
Expand All @@ -342,6 +352,25 @@ function MenuItem(
isDeleted ? styles.offlineFeedback.deleted : {},
]);

useEffect(() => {
if (!shouldFocusTemporarily) {
return;
}
setTemporaryilyFocused(true);
tempFocusTimeoutRef.current = setTimeout(() => setTemporaryilyFocused(false), temporaryFocusDuration);
}, [shouldFocusTemporarily, temporaryFocusDuration]);

useEffect(
() => () => {
if (!tempFocusTimeoutRef.current) {
return;
}

clearTimeout(tempFocusTimeoutRef.current);
},
[],
);
luacmartins marked this conversation as resolved.
Show resolved Hide resolved
luacmartins marked this conversation as resolved.
Show resolved Hide resolved

useEffect(() => {
if (!title || (titleRef.current.length && titleRef.current === title) || !shouldParseTitle) {
return;
Expand Down Expand Up @@ -417,7 +446,7 @@ function MenuItem(
errorText ? styles.pb5 : {},
combinedStyle,
!interactive && styles.cursorDefault,
StyleUtils.getButtonBackgroundColorStyle(getButtonState(focused || isHovered, pressed, success, disabled, interactive), true),
StyleUtils.getButtonBackgroundColorStyle(getButtonState(temporaryilyFocused || focused || isHovered, pressed, success, disabled, interactive), true),
!focused && (isHovered || pressed) && hoverAndPressStyle,
...(Array.isArray(wrapperStyle) ? wrapperStyle : [wrapperStyle]),
shouldGreyOutWhenDisabled && disabled && styles.buttonOpacityDisabled,
Expand Down
4 changes: 4 additions & 0 deletions src/pages/workspace/WorkspaceInitialPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ function WorkspaceInitialPage({policyDraft, policy: policyProp, policyMembers, r

const prevPolicy = usePrevious(policy);

const prevProtectedMenuItems = usePrevious(protectedCollectPolicyMenuItems);
const enabledItems = protectedCollectPolicyMenuItems.filter((curItem) => !prevProtectedMenuItems.some((prevItem) => curItem.translationKey === prevItem.translationKey));
luacmartins marked this conversation as resolved.
Show resolved Hide resolved

// eslint-disable-next-line rulesdir/no-negated-variables
const shouldShowNotFoundPage =
isEmptyObject(policy) ||
Expand Down Expand Up @@ -287,6 +290,7 @@ function WorkspaceInitialPage({policyDraft, policy: policyProp, policyMembers, r
brickRoadIndicator={item.brickRoadIndicator}
wrapperStyle={styles.sectionMenuItem}
focused={!!(item.routeName && activeRoute?.startsWith(item.routeName))}
shouldFocusTemporarily={enabledItems.length === 1 && enabledItems[0].translationKey === item.translationKey}
luacmartins marked this conversation as resolved.
Show resolved Hide resolved
hoverAndPressStyle={styles.hoveredComponentBG}
isPaneMenu
/>
Expand Down
Loading