Skip to content

Commit

Permalink
add start example
Browse files Browse the repository at this point in the history
  • Loading branch information
riccardoperra committed Nov 4, 2024
1 parent 9308a28 commit 830fe7f
Show file tree
Hide file tree
Showing 17 changed files with 3,691 additions and 95 deletions.
29 changes: 29 additions & 0 deletions examples/counter-start/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

dist
.solid
.output
.vercel
.netlify
.vinxi
app.config.timestamp_*.js

# Environment
.env
.env*.local

# dependencies
/node_modules

# IDEs and editors
/.idea
.project
.classpath
*.launch
.settings/

# Temp
gitignore

# System Files
.DS_Store
Thumbs.db
32 changes: 32 additions & 0 deletions examples/counter-start/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# SolidStart

Everything you need to build a Solid project, powered by [`solid-start`](https://start.solidjs.com);

## Creating a project

```bash
# create a new project in the current directory
npm init solid@latest

# create a new project in my-app
npm init solid@latest my-app
```

## Developing

Once you've created a project and installed dependencies with `npm install` (or `pnpm install` or `yarn`), start a development server:

```bash
npm run dev

# or start the server and open the app in a new browser tab
npm run dev -- --open
```

## Building

Solid apps are built with _presets_, which optimise your project for deployment to different environments.

By default, `npm run build` will generate a Node app that you can run with `npm start`. To use a different preset, add it to the `devDependencies` in `package.json` and specify in your `app.config.js`.

## This project was created with the [Solid CLI](https://solid-cli.netlify.app)
12 changes: 12 additions & 0 deletions examples/counter-start/app.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { defineConfig } from '@solidjs/start/config';
import { statebuilder } from 'statebuilder/vite';

export default defineConfig({
vite: {
plugins: [
statebuilder({
autoKey: true,
}),
],
},
});
21 changes: 21 additions & 0 deletions examples/counter-start/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "example-basic",
"type": "module",
"scripts": {
"dev": "vinxi dev",
"build": "vinxi build",
"start": "vinxi start",
"version": "vinxi version"
},
"dependencies": {
"@solidjs/meta": "^0.29.4",
"@solidjs/router": "^0.15.0",
"@solidjs/start": "^1.0.10",
"statebuilder": "workspace:*",
"solid-js": "^1.9.2",
"vinxi": "^0.4.3"
},
"engines": {
"node": ">=18"
}
}
Binary file added examples/counter-start/public/favicon.ico
Binary file not shown.
39 changes: 39 additions & 0 deletions examples/counter-start/src/app.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
body {
font-family: Gordita, Roboto, Oxygen, Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
}

a {
margin-right: 1rem;
}

main {
text-align: center;
padding: 1em;
margin: 0 auto;
}

h1 {
color: #335d92;
text-transform: uppercase;
font-size: 4rem;
font-weight: 100;
line-height: 1.1;
margin: 4rem auto;
max-width: 14rem;
}

p {
max-width: 14rem;
margin: 2rem auto;
line-height: 1.35;
}

@media (min-width: 480px) {
h1 {
max-width: none;
}

p {
max-width: none;
}
}
25 changes: 25 additions & 0 deletions examples/counter-start/src/app.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { MetaProvider, Title } from '@solidjs/meta';
import { Router } from '@solidjs/router';
import { FileRoutes } from '@solidjs/start/router';
import { Suspense } from 'solid-js';
import './app.css';
import { StateProvider } from 'statebuilder';

export default function App() {
return (
<Router
root={(props) => (
<MetaProvider>
<Title>SolidStart - Basic</Title>
<a href="/">Index</a>
<a href="/about">About</a>
<StateProvider>
<Suspense>{props.children}</Suspense>
</StateProvider>
</MetaProvider>
)}
>
<FileRoutes />
</Router>
);
}
21 changes: 21 additions & 0 deletions examples/counter-start/src/components/Counter.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
.increment {
font-family: inherit;
font-size: inherit;
padding: 1em 2em;
color: #335d92;
background-color: rgba(68, 107, 158, 0.1);
border-radius: 2em;
border: 2px solid rgba(68, 107, 158, 0);
outline: none;
width: 200px;
font-variant-numeric: tabular-nums;
cursor: pointer;
}

.increment:focus {
border: 2px solid #335d92;
}

.increment:active {
background-color: rgba(68, 107, 158, 0.2);
}
105 changes: 105 additions & 0 deletions examples/counter-start/src/components/Counter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import './Counter.css';
import { defineSignal, defineStore, provideState } from 'statebuilder';
import { withProxyCommands } from 'statebuilder/commands';
import { withReduxDevtools } from 'statebuilder/devtools';
import { withAsyncAction } from 'statebuilder/asyncAction';
import { withReducer } from 'statebuilder/reducer';

type Increment = { type: 'increment'; payload: number };

type Decrement = { type: 'decrement'; payload: number };

type AppActions = Increment | Decrement;

type AppState = {
count: number;
};

function appReducer(state: AppState, action: AppActions) {
switch (action.type) {
case 'increment':
return { ...state, count: state.count + action.payload };
case 'decrement':
return { ...state, count: state.count - action.payload };
default:
return state;
}
}

const GlobalCount = defineSignal(() => 1).extend((_, context) => {
context.hooks.onInit(() => console.log('init count2'));
context.hooks.onDestroy(() => console.log('destroy count2'));
});

const CountStore = defineStore(() => ({
count: 0,
}))
.extend(withAsyncAction())
.extend(
withProxyCommands<{ increment: void }>({
devtools: { storeName: 'counter' },
}),
)
.extend(withReduxDevtools({ storeName: 'countStore' }))
.extend(withReducer(appReducer))
.extend((state, context) => {
state.hold(state.commands.increment, () =>
state.set('count', (count) => count + 1),
);

context.hooks.onInit(() => console.log('init'));
context.hooks.onDestroy(() => console.log('destroy'));

return {
incrementAfter1S: state.asyncAction(async (payload: number) => {
await new Promise((r) => setTimeout(r, 3000));
state.set('count', (count) => count + 1);
return payload;
}),
};
});

function Counter() {
'use stateprovider';
const store = provideState(CountStore);
const globalCount = provideState(GlobalCount);

return (
<>
<button
class="increment"
onClick={() => globalCount.set((count) => count + 1)}
>
Global Count Clicks: {globalCount()}
</button>

<button
class="increment"
onClick={() => store.actions.increment()}
>
Clicks: {store().count}
</button>

<button
class="increment"
disabled={store.incrementAfter1S.loading}
onClick={() => store.incrementAfter1S()}
>
Increment after 1 sec
</button>

<button
class="increment"
onClick={() => store.dispatch({ type: 'increment', payload: 1 })}
>
Increment with reducer
</button>
</>
);
}

export default function CounterRoot() {
'use stateprovider';
const globalCount = provideState(GlobalCount);
return <Counter />;
}
4 changes: 4 additions & 0 deletions examples/counter-start/src/entry-client.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// @refresh reload
import { mount, StartClient } from "@solidjs/start/client";

mount(() => <StartClient />, document.getElementById("app")!);
21 changes: 21 additions & 0 deletions examples/counter-start/src/entry-server.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// @refresh reload
import { createHandler, StartServer } from "@solidjs/start/server";

export default createHandler(() => (
<StartServer
document={({ assets, children, scripts }) => (
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" href="/favicon.ico" />
{assets}
</head>
<body>
<div id="app">{children}</div>
{scripts}
</body>
</html>
)}
/>
));
1 change: 1 addition & 0 deletions examples/counter-start/src/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="@solidjs/start/env" />
19 changes: 19 additions & 0 deletions examples/counter-start/src/routes/[...404].tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Title } from "@solidjs/meta";
import { HttpStatusCode } from "@solidjs/start";

export default function NotFound() {
return (
<main>
<Title>Not Found</Title>
<HttpStatusCode code={404} />
<h1>Page Not Found</h1>
<p>
Visit{" "}
<a href="https://start.solidjs.com" target="_blank">
start.solidjs.com
</a>{" "}
to learn how to build SolidStart apps.
</p>
</main>
);
}
10 changes: 10 additions & 0 deletions examples/counter-start/src/routes/about.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Title } from "@solidjs/meta";

export default function Home() {
return (
<main>
<Title>About</Title>
<h1>About</h1>
</main>
);
}
19 changes: 19 additions & 0 deletions examples/counter-start/src/routes/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Title } from "@solidjs/meta";
import Counter from "~/components/Counter";

export default function Home() {
return (
<main>
<Title>Hello World</Title>
<h1>Hello world!</h1>
<Counter />
<p>
Visit{" "}
<a href="https://start.solidjs.com" target="_blank">
start.solidjs.com
</a>{" "}
to learn how to build SolidStart apps.
</p>
</main>
);
}
19 changes: 19 additions & 0 deletions examples/counter-start/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "bundler",
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"jsx": "preserve",
"jsxImportSource": "solid-js",
"allowJs": true,
"strict": true,
"noEmit": true,
"types": ["vinxi/types/client"],
"isolatedModules": true,
"paths": {
"~/*": ["./src/*"]
}
}
}
Loading

0 comments on commit 830fe7f

Please sign in to comment.