Skip to content

Commit

Permalink
chore: fix stitches issues
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuaellis committed May 11, 2024
1 parent 454a771 commit 487bdb5
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 7 deletions.
3 changes: 1 addition & 2 deletions docs/app/components/Feedback/Feedback.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export const Feedback = ({ location }: FeedbackProps) => {
const isDarkMode = useIsDarkTheme()

const handleClick = (type: 'upvote' | 'downvote') => () => {
console.log('clicked!')
setSelected(type)

const name =
Expand Down Expand Up @@ -146,7 +145,7 @@ const FeedbackButton = ({
<X />
</PopoverClose>
</PopoverHeader>
<Form ref={formRef} method="post" action="/api/feedback" replace>
<Form ref={formRef} method="post" action="/api/feedback">
<HiddenInput
name="variant"
type="checkbox"
Expand Down
30 changes: 28 additions & 2 deletions docs/app/entry.client.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
import { RemixBrowser } from '@remix-run/react'
import * as React from 'react'
import { hydrateRoot } from 'react-dom/client'

import { RemixBrowser } from '@remix-run/react'
import { ClientStyleContext } from '~/styles/client.context'
import { getCssText } from '~/styles/stitches.config'

interface ClientCacheProviderProps {
children: React.ReactNode
}

function ClientCacheProvider({ children }: ClientCacheProviderProps) {
const [sheet, setSheet] = React.useState(getCssText())

const reset = React.useCallback(() => {
setSheet(getCssText())
}, [])

return (
<ClientStyleContext.Provider value={{ reset, sheet }}>
{children}
</ClientStyleContext.Provider>
)
}

hydrateRoot(document, <RemixBrowser />)
hydrateRoot(
document,
<ClientCacheProvider>
<RemixBrowser />
</ClientCacheProvider>
)
9 changes: 7 additions & 2 deletions docs/app/entry.server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ export default function handleRequest(
responseHeaders: Headers,
remixContext: EntryContext
) {
const markup = renderToString(
let markup = renderToString(
<RemixServer context={remixContext} url={request.url} />
).replace(/<\/head>/, `<style id="stitches">${getCssText()}</style></head>`)
)

markup = markup.replace(
/<style id="stitches">.*<\/style>/g,
`<style id="stitches">${getCssText()}</style>`
)

responseHeaders.set('Content-Type', 'text/html')

Expand Down
18 changes: 17 additions & 1 deletion docs/app/root.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as React from 'react'

import {
MetaFunction,
LinksFunction,
Expand All @@ -20,6 +22,7 @@ import { globalStyles } from './styles/global'
import { WidgetTheme } from './components/Widgets/WidgetTheme'
import { WidgetPlausible } from './components/Widgets/WidgetPlausible'
import { SiteFooter } from './components/Site/SiteFooter'
import { ClientStyleContext } from './styles/client.context'

export const meta: MetaFunction = () => {
return [
Expand Down Expand Up @@ -82,7 +85,15 @@ export const loader: LoaderFunction = () => {
function Document({ children }: { children: React.ReactNode }) {
globalStyles()

const data = useLoaderData()
const clientStyleData = React.useContext(ClientStyleContext)

// Only executed on client
React.useEffect(() => {
// reset cache to re-apply global styles
clientStyleData.reset()
}, [clientStyleData])

const data = useLoaderData<{ ENV: object }>()

return (
<html lang="en">
Expand All @@ -91,6 +102,11 @@ function Document({ children }: { children: React.ReactNode }) {
<meta name="viewport" content="width=device-width,initial-scale=1" />
<Meta />
<Links />
<style
id="stitches"
dangerouslySetInnerHTML={{ __html: clientStyleData.sheet }}
suppressHydrationWarning
/>
<WidgetPlausible />
<WidgetTheme />
</head>
Expand Down
13 changes: 13 additions & 0 deletions docs/app/styles/client.context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { createContext } from 'react'

export interface ClientStyleContextData {
reset: () => void
sheet: string
}

const ClientStyleContext = createContext<ClientStyleContextData>({
reset: () => {},
sheet: '',
})

export { ClientStyleContext }

0 comments on commit 487bdb5

Please sign in to comment.