Skip to content

Commit

Permalink
RSC: Add css files to the example (#8905)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobbe authored Jul 14, 2023
1 parent 2a92ac3 commit fcd7c39
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 6 deletions.
35 changes: 35 additions & 0 deletions packages/cli/src/commands/experimental/setupRscHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,41 @@ export const handler = async ({ force, verbose }) => {
})
},
},
{
title: 'Adding CSS files...',
task: async () => {
const files = [
{
template: 'Counter.css.template',
path: 'Counter.css',
},
{
template: 'Counter.module.css.template',
path: 'Counter.module.css',
},
{
template: 'App.css.template',
path: 'App.css',
},
{
template: 'App.module.css.template',
path: 'App.module.css',
},
]

files.forEach((file) => {
const template = fs.readFileSync(
path.resolve(__dirname, 'templates', 'rsc', file.template),
'utf-8'
)
const filePath = path.join(rwPaths.web.src, file.path)

writeFile(filePath, template, {
overwriteExisting: force,
})
})
},
},
{
title: 'Updating index.html...',
task: async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
h1 {
text-decoration: underline;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.title {
color: green;
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
import { Assets } from '@redwoodjs/vite/assets'
import { ProdRwRscServerGlobal } from '@redwoodjs/vite/rwRscGlobal'

// @ts-expect-error no types
import styles from './App.module.css'
import { Counter } from './Counter'

import './App.css'

// TODO (RSC) Something like this will probably be needed
// const RwRscGlobal = import.meta.env.PROD ? ProdRwRscServerGlobal : DevRwRscServerGlobal;

globalThis.rwRscGlobal = new ProdRwRscServerGlobal()

const App = ({ name = 'Anonymous' }) => {
return (
<div style={{ border: '3px red dashed', margin: '1em', padding: '1em' }}>
<h1>Hello {name}!!</h1>
<h3>This is a server component.</h3>
<Counter />
</div>
<>
{/* TODO (RSC) <Assets /> should be part of the router later */}
<Assets />
<div style={{ border: '3px red dashed', margin: '1em', padding: '1em' }}>
<h1 className={styles.title}>Hello {name}!!</h1>
<h3>This is a server component.</h3>
<Counter />
</div>
</>
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* This should affect all h3 elements on the page, both server components and
* client components. This is just standard CSS stuff
*/
h3 {
color: orange;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.header {
font-style: italic;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

import React from 'react'

// @ts-expect-error no types
import styles from './Counter.module.css'
import './Counter.css'

export const Counter = () => {
const [count, setCount] = React.useState(0)

return (
<div style={{ border: '3px blue dashed', margin: '1em', padding: '1em' }}>
<p>Count: {count}</p>
<button onClick={() => setCount((c) => c + 1)}>Increment</button>
<h3>This is a client component.</h3>
<h3 className={styles.header}>This is a client component.</h3>
</div>
)
}

0 comments on commit fcd7c39

Please sign in to comment.