Skip to content

Commit

Permalink
Keep panel state
Browse files Browse the repository at this point in the history
  • Loading branch information
jorbuedo committed May 30, 2021
1 parent 520fb4f commit 30bf826
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 3 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ Simple example app using Nextjs and Twind (JIT Tailwind lib).

Creates a gorcery shop with some products, cart, favorites and orders history.

## Live

Use the live version at: https://next-groceries-gamma.vercel.app/

**Install**

```
Expand Down
4 changes: 4 additions & 0 deletions components/Header/TogglePanel.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { panelAtom } from 'components/Panel/Panel'
import { useAtom } from 'jotai'
import { tw } from 'twind'
import { useIsClient } from 'utils/client'
import { linkStyle } from './NavLink'

export type TogglePanelProps = {
Expand All @@ -10,9 +11,12 @@ export type TogglePanelProps = {

export default function TogglePanel({ label, icon: Icon }: TogglePanelProps) {
const [isOpen, setIsOpen] = useAtom(panelAtom)
const { key } = useIsClient()

return (
<button
key={key}
suppressHydrationWarning
type="button"
className={tw`${linkStyle} hidden md:flex focus:outline-none ${
isOpen && 'bg-blue-200'
Expand Down
10 changes: 8 additions & 2 deletions components/Panel/Panel.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { tw } from 'twind'
import { css } from 'twind/css'
import { mainHeight } from 'styles'
import { atom, useAtom } from 'jotai'
import { useAtom } from 'jotai'
import { atomWithLocalStorage } from 'utils/atom'
import { useIsClient } from 'utils/client'

type PanelProps = {
children: React.ReactNode
Expand All @@ -12,12 +14,16 @@ const hideScrollBar = css({
'-webkit-scrollbar': { width: 'thin' },
})

export const panelAtom = atom(false)
export const panelAtom = atomWithLocalStorage<boolean>('Panel', false)

export default function Panel({ children }: PanelProps) {
const [isOpen] = useAtom(panelAtom)
const { key } = useIsClient()

return (
<div
key={key}
suppressHydrationWarning
className={tw`${
isOpen && 'md:block'
} bg-white hidden w-full max-w-sm relative`}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
},
"dependencies": {
"@twind/line-clamp": "^0.1.1",
"@twind/next": "^1.0.7",
"immer": "^9.0.2",
"jotai": "^0.16.5",
"nanoid": "^3.1.23",
Expand Down
3 changes: 2 additions & 1 deletion pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Head from 'next/head'
import Header from 'components/Header'
import ScrollWatcher from 'components/ScrollWatcher'
import Toaster from 'components/Toaster'
import withTwindApp from '@twind/next/app'

if (process.env.NODE_ENV === 'development') {
import('twind/shim')
Expand All @@ -32,4 +33,4 @@ function App({ Component, pageProps }: AppProps) {
)
}

export default App
export default withTwindApp(App)
12 changes: 12 additions & 0 deletions utils/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { useState, useEffect } from 'react'

export const useIsClient = () => {
const [isClient, setClient] = useState(false)
const key = isClient ? 'client' : 'server'

useEffect(() => {
setClient(true)
}, [])

return { isClient, key }
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,11 @@
resolved "https://registry.yarnpkg.com/@twind/line-clamp/-/line-clamp-0.1.1.tgz#34c82fbb89864a24bc3f603b47d6b5ad48a669ed"
integrity sha512-OAbGo1traJ5KmTSlIGPAAg8RnQx3XCGriKyPJA2L6yuO0bO1DwDm6oIAAYuLuV792NDyfWeZNcwKUfBXWSnDVQ==

"@twind/next@^1.0.7":
version "1.0.7"
resolved "https://registry.yarnpkg.com/@twind/next/-/next-1.0.7.tgz#d3900261bd0fe0ad102ddec2ca2d9b8e2d6c08e4"
integrity sha512-hEVsZJK0t1FH44TH0Al7GLXXaEW8H35DgCIu69ndI5ltAFOTyZ8ioqBbx+EiedU9cBQ66iA5QdahxuFdj6XoXA==

"@types/aria-query@^4.2.0":
version "4.2.1"
resolved "https://registry.yarnpkg.com/@types/aria-query/-/aria-query-4.2.1.tgz#78b5433344e2f92e8b306c06a5622c50c245bf6b"
Expand Down

1 comment on commit 30bf826

@vercel
Copy link

@vercel vercel bot commented on 30bf826 May 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.