-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
replace show hide buttons with islands. remove old show hide page lev…
…el island
- Loading branch information
Showing
8 changed files
with
120 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
dotcom-rendering/src/components/ShowHideButton.importable.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { css } from '@emotion/react'; | ||
import { isObject, isString, storage } from '@guardian/libs'; | ||
import { space, textSans14 } from '@guardian/source/foundations'; | ||
import { Button } from '@guardian/source/react-components'; | ||
import { useEffect, useState } from 'react'; | ||
import { useIsSignedIn } from '../lib/useAuthStatus'; | ||
import { palette } from '../palette'; | ||
|
||
type Props = { | ||
sectionId: string; | ||
}; | ||
|
||
const showHideButtonCss = css` | ||
button { | ||
${textSans14}; | ||
/* margin-top: ${space[2]}px; */ | ||
margin-right: 10px; | ||
margin-bottom: ${space[2]}px; | ||
position: relative; | ||
align-items: bottom; | ||
text-decoration: none; | ||
} | ||
`; | ||
|
||
type ContainerStates = { [id: string]: string }; | ||
|
||
const isContainerStates = (item: unknown): item is ContainerStates => { | ||
if (!isObject(item)) return false; | ||
if (!Object.keys(item).every(isString)) return false; | ||
if (!Object.values(item).every(isString)) return false; | ||
return true; | ||
}; | ||
|
||
const getContainerStates = (): ContainerStates => { | ||
const item = storage.local.get(`gu.prefs.container-states`); | ||
if (!isContainerStates(item)) return {}; | ||
return item; | ||
}; | ||
|
||
/** | ||
* Component to toggle the visibility of a front container. Used within FrontSection. | ||
**/ | ||
export const ShowHideButton = ({ sectionId }: Props) => { | ||
const [isExpanded, setIsExpanded] = useState(true); | ||
const [containerStates, setContainerStates] = useState<ContainerStates>({}); | ||
const textShowHide = isExpanded ? 'Hide' : 'Show'; | ||
const isSignedIn = useIsSignedIn(); | ||
|
||
const toggleContainer = () => { | ||
const section: Element | null = | ||
window.document.getElementById(sectionId); | ||
|
||
if (isExpanded) { | ||
containerStates[sectionId] = 'closed'; | ||
section?.classList.add('hidden'); | ||
} else { | ||
containerStates[sectionId] = 'opened'; | ||
section?.classList.remove('hidden'); | ||
} | ||
|
||
storage.local.set(`gu.prefs.container-states`, containerStates); | ||
setIsExpanded(!isExpanded); | ||
}; | ||
|
||
useEffect(() => { | ||
setContainerStates(getContainerStates()); | ||
}, []); | ||
|
||
return ( | ||
isSignedIn === true && ( | ||
<div css={showHideButtonCss}> | ||
<Button | ||
priority="subdued" | ||
data-link-name={textShowHide} | ||
data-show-hide-button={sectionId} | ||
aria-controls={sectionId} | ||
aria-expanded={isExpanded} | ||
theme={{ | ||
textSubdued: palette('--section-toggle-button'), | ||
}} | ||
onClick={toggleContainer} | ||
> | ||
{textShowHide} | ||
</Button> | ||
</div> | ||
) | ||
); | ||
}; |
14 changes: 14 additions & 0 deletions
14
dotcom-rendering/src/components/ShowHideButton.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import type { Meta } from '@storybook/react'; | ||
import { ShowHideButton } from './ShowHideButton.importable'; | ||
|
||
const meta = { | ||
title: 'Components/ShowHideButton', | ||
component: ShowHideButton, | ||
args: { | ||
sectionId: 'sectionId', | ||
}, | ||
} satisfies Meta<typeof ShowHideButton>; | ||
|
||
export default meta; | ||
|
||
export const Default = {}; |
This file was deleted.
Oops, something went wrong.
88 changes: 0 additions & 88 deletions
88
dotcom-rendering/src/components/ShowHideContainers.importable.tsx
This file was deleted.
Oops, something went wrong.