-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs(example): add an example of using emotion css prop #1607
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1538735
duplicate example template
himorishige 51489c3
add: emotion css prop example
himorishige e36dd31
update: comment
himorishige 8ab7b8c
Update contributors.yml
himorishige bd5902b
change the use of LiveReaload
himorishige c462154
fix emotion provider
himorishige 5b66ce7
add: Error and Catch Boundary functions
himorishige e0028f3
add: sub page and link
himorishige File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,6 +58,7 @@ | |
- GregBrimble | ||
- hardingmatt | ||
- HenryVogt | ||
- himorishige | ||
- hkan | ||
- Holben888 | ||
- hollandThomas | ||
|
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,5 @@ | ||
node_modules | ||
|
||
/.cache | ||
/build | ||
/public/build |
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,26 @@ | ||
# Example app with [Emotion](https://emotion.sh/docs/introduction) - css Prop | ||
|
||
This example features how to use [Emotion](https://emotion.sh/docs/introduction) - css Prop with Remix. | ||
|
||
## Preview | ||
|
||
Open this example on [CodeSandbox](https://codesandbox.com): | ||
|
||
[![Open in CodeSandbox](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/github/remix-run/remix/tree/main/examples/emotion-css-prop) | ||
|
||
## Example | ||
|
||
This example shows how to use Emotion - css Prop with Remix. | ||
|
||
Relevant files: | ||
|
||
- [app/root.tsx](./app/root.tsx) - This is where we render the app and if we're rendering on the server we have placeholder text of __STYLES__. | ||
- [app/entry.server.tsx](./app/entry.server.tsx) - This is where we render the app on the server and replace __STYLES__ with the styles that emotion collect. | ||
- [app/routes/index.tsx](./app/routes/index.tsx) - Here's where we use the css Prop to styling component. | ||
- [tsconfig.json](./tsconfig.json) - Add `jsxImportSource` to use the css Prop in tsx file. | ||
|
||
## Related Links | ||
|
||
- [Emotion](https://emotion.sh/docs/introduction) | ||
- [The css Prop](https://emotion.sh/docs/css-prop) | ||
- [emotion-reset](https://github.com/sayegh7/emotion-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 @@ | ||
import { hydrate } from "react-dom"; | ||
import { RemixBrowser } from "remix"; | ||
|
||
hydrate(<RemixBrowser />, document); |
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,56 @@ | ||
import createCache from "@emotion/cache"; | ||
import { CacheProvider, css, Global } from "@emotion/react"; | ||
import createEmotionServer from "@emotion/server/create-instance"; | ||
import emotionReset from "emotion-reset"; | ||
import { renderToString } from "react-dom/server"; | ||
import type { EntryContext } from "remix"; | ||
import { RemixServer } from "remix"; | ||
import { StylesProvider } from "./styles-context"; | ||
|
||
const key = "emotion"; | ||
const cache = createCache({ key }); | ||
const { extractCriticalToChunks, constructStyleTagsFromChunks } = | ||
createEmotionServer(cache); | ||
|
||
export default function handleRequest( | ||
request: Request, | ||
responseStatusCode: number, | ||
responseHeaders: Headers, | ||
remixContext: EntryContext | ||
) { | ||
const html = renderToString( | ||
<CacheProvider value={cache}> | ||
<StylesProvider value={null}> | ||
<RemixServer context={remixContext} url={request.url} /> | ||
</StylesProvider> | ||
</CacheProvider> | ||
); | ||
|
||
const chunks = extractCriticalToChunks(html); | ||
const styles = constructStyleTagsFromChunks(chunks); | ||
|
||
const markup = renderToString( | ||
<StylesProvider value={styles}> | ||
{/* Set the global style. */} | ||
<Global | ||
styles={css` | ||
${emotionReset} | ||
*, *::after, *::before { | ||
box-sizing: border-box; | ||
-moz-osx-font-smoothing: grayscale; | ||
-webkit-font-smoothing: antialiased; | ||
font-smoothing: antialiased; | ||
} | ||
`} | ||
/> | ||
<RemixServer context={remixContext} url={request.url} /> | ||
</StylesProvider> | ||
); | ||
|
||
responseHeaders.set("Content-Type", "text/html"); | ||
|
||
return new Response("<!DOCTYPE html>" + markup, { | ||
status: responseStatusCode, | ||
headers: responseHeaders | ||
}); | ||
} |
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,51 @@ | ||
import { | ||
Links, | ||
LiveReload, | ||
Meta, | ||
Outlet, | ||
Scripts, | ||
ScrollRestoration, | ||
useCatch | ||
} from "remix"; | ||
import { useStyles } from "./styles-context"; | ||
|
||
export default function App() { | ||
const styles = useStyles(); | ||
return ( | ||
<html lang="en"> | ||
<head> | ||
<meta charSet="utf-8" /> | ||
<meta name="viewport" content="width=device-width,initial-scale=1" /> | ||
<Meta /> | ||
<Links /> | ||
{typeof document === "undefined" ? null : styles} | ||
</head> | ||
<body> | ||
<Outlet /> | ||
<ScrollRestoration /> | ||
<Scripts /> | ||
<LiveReload /> | ||
</body> | ||
</html> | ||
); | ||
} | ||
|
||
export function CatchBoundary() { | ||
const caught = useCatch(); | ||
if (caught.status === 404) { | ||
return ( | ||
<div> | ||
<h1>{caught.statusText}</h1> | ||
</div> | ||
); | ||
} | ||
throw new Error(`Unhandled error: ${caught.status}`); | ||
} | ||
|
||
export function ErrorBoundary({ error }: { error: Error }) { | ||
return ( | ||
<div> | ||
<h1>{error.message}</h1> | ||
</div> | ||
); | ||
} |
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,46 @@ | ||
/** @jsx jsx */ | ||
import { css, jsx } from "@emotion/react"; | ||
import { useEffect, useState } from "react"; | ||
import { Link, useCatch } from "remix"; | ||
|
||
// Object Styles | ||
const wrapperStyle = { | ||
height: "100vh", | ||
display: "flex", | ||
alignItems: "center", | ||
justifyContent: "center" | ||
}; | ||
|
||
// String Styles | ||
const headingStyle = css` | ||
font-size: 2rem; | ||
color: hotpink; | ||
transition: color 0.5s 0s ease; | ||
`; | ||
|
||
const headingDefaultStyle = css` | ||
font-size: 2rem; | ||
`; | ||
|
||
export default function Index() { | ||
const [state, setState] = useState(false); | ||
|
||
useEffect(() => { | ||
new Promise(resolve => setTimeout(resolve, 1000)).then(() => { | ||
setState(true); | ||
}); | ||
}, []); | ||
|
||
return ( | ||
<div> | ||
<header> | ||
<Link to="/">Top Page</Link> | <Link to="/sub">Sub Page</Link> | ||
</header> | ||
<div css={wrapperStyle}> | ||
<h1 css={state ? headingStyle : headingDefaultStyle}> | ||
Welcome to Remix (With Emotion css Prop) | ||
</h1> | ||
</div> | ||
</div> | ||
); | ||
} |
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,46 @@ | ||
/** @jsx jsx */ | ||
import { css, jsx } from "@emotion/react"; | ||
import { useEffect, useState } from "react"; | ||
import { Link } from "remix"; | ||
|
||
// Object Styles | ||
const wrapperStyle = { | ||
height: "100vh", | ||
display: "flex", | ||
alignItems: "center", | ||
justifyContent: "center" | ||
}; | ||
|
||
// String Styles | ||
const headingStyle = css` | ||
font-size: 2rem; | ||
color: blue; | ||
transition: color 0.5s 0s ease; | ||
`; | ||
|
||
const headingDefaultStyle = css` | ||
font-size: 2rem; | ||
`; | ||
|
||
export default function Index() { | ||
const [state, setState] = useState(false); | ||
|
||
useEffect(() => { | ||
new Promise(resolve => setTimeout(resolve, 1000)).then(() => { | ||
setState(true); | ||
}); | ||
}, []); | ||
|
||
return ( | ||
<div> | ||
<header> | ||
<Link to="/">Top Page</Link> | <Link to="/sub">Sub Page</Link> | ||
</header> | ||
<div css={wrapperStyle}> | ||
<h1 css={state ? headingStyle : headingDefaultStyle}> | ||
Welcome to SubPage (With Emotion css Prop) | ||
</h1> | ||
</div> | ||
</div> | ||
); | ||
} |
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 @@ | ||
import * as React from "react"; | ||
const StylesContext = React.createContext<null | React.ReactNode>(null); | ||
export const StylesProvider = StylesContext.Provider; | ||
export const useStyles = () => React.useContext(StylesContext); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does esbuild respect this pragma?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
esbuild supports jsx annotations.
However, it seems that jsxImportSource support is not yet implemented without babel support.
ref:
Support JSX annotation
evanw/esbuild#138
Support /** @jsx pragma
evanw/esbuild#389
Add support for jsxImportSource
evanw/esbuild#1172
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although it is limited, it seems to be possible to remove the pragma by adopting the patch in this issue.