-
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #498 from edkimmel/main
Web fixes and server support for 3.0.0
- Loading branch information
Showing
12 changed files
with
84 additions
and
231 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import React from 'react' | ||
import { StyleSheet } from 'react-native' | ||
import { error, isServer } from '../web/utils' | ||
import { UnistylesWeb } from '../web' | ||
import { DefaultServerUnistylesSettings, type ServerUnistylesSettings } from './types' | ||
|
||
export const getServerUnistyles = ({ includeRNWStyles = true }: ServerUnistylesSettings = DefaultServerUnistylesSettings) => { | ||
if (!isServer()) { | ||
throw error('Server styles should only be read on the server') | ||
} | ||
// @ts-ignore | ||
const rnwStyle: string | null = includeRNWStyles ? (StyleSheet?.getSheet().textContent ?? '') : null | ||
const css = UnistylesWeb.registry.css.getStyles() | ||
const state = UnistylesWeb.registry.css.getState() | ||
return <> | ||
{rnwStyle && <style id='rnw-style'>{rnwStyle}</style>} | ||
<style id='unistyles-web'>{css}</style> | ||
<script id='unistyles-script'>{`window.__UNISTYLES_STATE__ = ${JSON.stringify(state)}`}</script> | ||
</> | ||
} |
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,17 @@ | ||
import { error, isServer } from '../web/utils' | ||
import { UnistylesWeb } from '../web' | ||
|
||
declare global { | ||
interface Window { | ||
// @ts-ignore | ||
__UNISTYLES_STATE__: ReturnType<typeof UnistylesWeb.registry.css.getState> | ||
} | ||
} | ||
|
||
export const hydrateServerUnistyles = () => { | ||
if (isServer()) { | ||
throw error('Server styles should only be hydrated on the client') | ||
} | ||
UnistylesWeb.registry.css.hydrate(window.__UNISTYLES_STATE__) | ||
document.querySelector('#unistyles-script')?.remove() | ||
} |
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 |
---|---|---|
@@ -1 +1,5 @@ | ||
export { useServerUnistyles } from './useServerUnistyles' | ||
export { getServerUnistyles } from './getServerUnistyles' | ||
export { resetServerUnistyles } from './resetServerUnistyles' | ||
export { hydrateServerUnistyles } from './hydrateServerUnistyles' | ||
|
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,9 @@ | ||
import { error, isServer } from '../web/utils' | ||
import { UnistylesWeb } from '../web' | ||
|
||
export const resetServerUnistyles = () => { | ||
if (!isServer()) { | ||
throw error('Server styles should only be reset on the server') | ||
} | ||
UnistylesWeb.registry.reset() | ||
} |
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,4 @@ | ||
export type ServerUnistylesSettings = { | ||
includeRNWStyles?: boolean | ||
} | ||
export const DefaultServerUnistylesSettings = {} |
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 |
---|---|---|
@@ -1,42 +1,22 @@ | ||
import React, { useRef } from 'react' | ||
import { StyleSheet } from 'react-native' | ||
import { useServerInsertedHTML } from 'next/navigation' | ||
import { UnistylesWeb } from '../web' | ||
import { isServer } from '../web/utils' | ||
import { DefaultServerUnistylesSettings, type ServerUnistylesSettings } from './types' | ||
import { getServerUnistyles } from './getServerUnistyles' | ||
import { resetServerUnistyles } from './resetServerUnistyles' | ||
import { hydrateServerUnistyles } from './hydrateServerUnistyles' | ||
|
||
declare global { | ||
interface Window { | ||
// @ts-ignore | ||
__UNISTYLES_STATE__: ReturnType<typeof UnistylesWeb.registry.css.getState> | ||
} | ||
} | ||
|
||
export const useServerUnistyles = () => { | ||
export const useServerUnistyles = (settings: ServerUnistylesSettings = DefaultServerUnistylesSettings): React.ReactNode | null => { | ||
const isServerInserted = useRef(false) | ||
|
||
useServerInsertedHTML(() => { | ||
if (!isServerInserted.current) { | ||
isServerInserted.current = true | ||
|
||
// @ts-ignore | ||
const rnwStyle = StyleSheet?.getSheet().textContent ?? '' | ||
const css = UnistylesWeb.registry.css.getStyles() | ||
const state = UnistylesWeb.registry.css.getState() | ||
UnistylesWeb.registry.reset() | ||
|
||
return ( | ||
<> | ||
<style id='rnw-style'>{rnwStyle}</style> | ||
<style id='unistyles-web'>{css}</style> | ||
<script id='unistyles-script'>{`window.__UNISTYLES_STATE__ = ${JSON.stringify(state)}`}</script> | ||
</> | ||
) | ||
} | ||
|
||
return null | ||
}) | ||
if (isServer() && !isServerInserted.current) { | ||
isServerInserted.current = true | ||
const components = getServerUnistyles(settings) | ||
resetServerUnistyles() | ||
return components | ||
} | ||
|
||
if (typeof window !== 'undefined') { | ||
UnistylesWeb.registry.css.hydrate(window.__UNISTYLES_STATE__) | ||
document.querySelector('#unistyles-script')?.remove() | ||
if (!isServer()) { | ||
hydrateServerUnistyles() | ||
} | ||
return null | ||
} |
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
Oops, something went wrong.