Skip to content

Commit

Permalink
move correction to tab context
Browse files Browse the repository at this point in the history
  • Loading branch information
seungpark committed Oct 9, 2024
1 parent 61f4707 commit 9f25901
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 30 deletions.
33 changes: 5 additions & 28 deletions src/components/Tabs/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import React, { useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react';
import { isEmpty } from 'lodash';
import React, { useCallback, useContext, useEffect, useRef, useState } from 'react';
import PropTypes from 'prop-types';
import { css, cx } from '@leafygreen-ui/emotion';
import { Tabs as LeafyTabs, Tab as LeafyTab } from '@leafygreen-ui/tabs';
import { palette } from '@leafygreen-ui/palette';
import { CodeProvider } from '../Code/code-context';
import ComponentFactory from '../ComponentFactory';
import { ContentsContext } from '../Contents/contents-context';
import { HeadingContextProvider, useHeadingContext } from '../../context/heading-context';
import { theme } from '../../theme/docsTheme';
import { reportAnalytics } from '../../utils/report-analytics';
Expand Down Expand Up @@ -105,13 +103,9 @@ const productLandingTabContentStyling = css`
}
`;

const activeMethodIncludesActiveTab = (activeTabValues, activeTabsSelector) =>
activeTabValues.every((tabValue) => activeTabsSelector['tab']?.includes(tabValue));

const Tabs = ({ nodeData: { children, options = {} }, page, ...rest }) => {
const { activeTabs, selectors, setActiveTab } = useContext(TabContext);
const tabIds = children.map((child) => getTabId(child));
const { activeSelectorIds, setActiveSelectorIds } = useContext(ContentsContext);
const tabsetName = options.tabset || generateAnonymousTabsetName(tabIds);
const [activeTab, setActiveTabIndex] = useState(() => {
// activeTabIdx at build time should be -1 if tabsetName !== drivers
Expand All @@ -120,26 +114,6 @@ const Tabs = ({ nodeData: { children, options = {} }, page, ...rest }) => {
return activeTabIdx > -1 ? activeTabIdx : 0;
});

const correctedSelectorIds = useMemo(() => {
if (isEmpty(activeTabs)) {
return;
}
if (!activeMethodIncludesActiveTab(Object.values(activeTabs), activeSelectorIds)) {
return {
...activeSelectorIds,
tab: Object.values(activeTabs),
};
}
}, [activeSelectorIds, activeTabs]);

useEffect(() => {
if (!correctedSelectorIds) {
return;
}

setActiveSelectorIds(correctedSelectorIds);
}, [correctedSelectorIds, setActiveSelectorIds]);

const scrollAnchorRef = useRef();
// Hide tabset if it includes the :hidden: option, or if it is controlled by a dropdown selector
const isHidden = options.hidden || Object.keys(selectors).includes(tabsetName);
Expand Down Expand Up @@ -169,6 +143,9 @@ const Tabs = ({ nodeData: { children, options = {} }, page, ...rest }) => {

const handleClick = useCallback(
(index) => {
if (activeTab === index) {
return;
}
const tabId = tabIds[index];
const priorAnchorOffset = getPosition(scrollAnchorRef.current).y;

Expand All @@ -183,7 +160,7 @@ const Tabs = ({ nodeData: { children, options = {} }, page, ...rest }) => {
window.scrollTo(0, getPosition(scrollAnchorRef.current).y + window.scrollY - priorAnchorOffset);
}, 40);
},
[setActiveTab, tabIds, tabsetName] // eslint-disable-line react-hooks/exhaustive-deps
[activeTab, setActiveTab, tabIds, tabsetName]
);

return (
Expand Down
15 changes: 13 additions & 2 deletions src/components/Tabs/tab-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
* child components to read and update
*/

import React, { useEffect, useReducer, useRef } from 'react';
import React, { useContext, useEffect, useReducer, useRef } from 'react';
import { isEmpty } from 'lodash';
import { getLocalValue, setLocalValue } from '../../utils/browser-storage';
import { DRIVER_ICON_MAP } from '../icons/DriverIconMap';
import { ContentsContext } from '../Contents/contents-context';
import { makeChoices } from './make-choices';

const defaultContextValue = {
Expand Down Expand Up @@ -54,14 +56,23 @@ const getLocalTabs = (localTabs, selectors) =>
const TabProvider = ({ children, selectors = {} }) => {
// init value to {} to match server and client side
const [activeTabs, setActiveTab] = useReducer(reducer, {});
const { setActiveSelectorIds } = useContext(ContentsContext);

const initLoaded = useRef(false);

useEffect(() => {
// dont update local value on initial load
if (!initLoaded.current) return;
setLocalValue('activeTabs', activeTabs);
}, [activeTabs]);

if (isEmpty(activeTabs)) {
return;
}

// on Tab update, update the active selector ids
// so headings can be shown/hidden
setActiveSelectorIds((activeSelectorIds) => ({ ...activeSelectorIds, tab: Object.values(activeTabs) }));
}, [activeTabs, setActiveSelectorIds]);

// initial effect to read from local storage
// used in an effect to keep SSG HTML consistent
Expand Down

0 comments on commit 9f25901

Please sign in to comment.