### Run example commands Source: https://github.com/solidjs/solid/blob/main/packages/solid-ssr/README.md Commands to build and start the SSR example using lerna. ```bash lerna run build:example:ssr --stream lerna run start:example:ssr --stream ``` -------------------------------- ### Install Solid Element and Dependencies Source: https://github.com/solidjs/solid/blob/main/packages/solid-element/README.md Install the necessary packages for Solid Element, Solid.js, and Babel preset for Solid. ```sh npm i solid-element solid-js babel-preset-solid ``` -------------------------------- ### Install SolidJS dependencies Source: https://github.com/solidjs/solid/blob/main/packages/solid/README.md Commands to install the necessary packages for a manual SolidJS setup. ```sh > npm i -D babel-preset-solid > npm i solid-js ``` -------------------------------- ### Render a component with html Source: https://github.com/solidjs/solid/blob/main/packages/solid/html/README.md Full example demonstrating component definition and rendering using html. ```js import { render } from "solid-js/web"; import html from "solid-js/html"; import { createSignal } from "solid-js"; function Button(props) { return html`` // create a component with a title prop html`<${Button} title="My button">Click me` // create an element with dynamic attribute and spread html`
selectedClass()} ...${props} />` ``` -------------------------------- ### Configure Babel Preset Source: https://github.com/solidjs/solid/blob/main/packages/babel-preset-solid/README.md Define the solid preset in your .babelrc or package.json configuration. ```json { "presets": [ "solid" ] } ``` ```json ... "babel": { "presets": [ "es2015", "solid" ], "plugins": [ ] }, ... ``` -------------------------------- ### Create Elements with HyperScript Source: https://github.com/solidjs/solid/blob/main/packages/solid/h/README.md Demonstrates basic usage of the HyperScript function for creating elements with attributes, components, and multiple children. ```javascript h("button", { title: "My button" }, "Click Me") ``` ```javascript h(Button, { title: "My button" }, "Click Me") ``` ```javascript h("div", { title: "My button" }, h("span", "1"), h("span", "2"), h("span", "3")) ``` -------------------------------- ### Configure Babel preset Source: https://github.com/solidjs/solid/blob/main/packages/solid/README.md Configuration snippet for adding the Solid preset to your Babel configuration. ```js "presets": ["solid"] ``` -------------------------------- ### Create a Counter Component in SolidJS Source: https://github.com/solidjs/solid/blob/main/README.md Demonstrates creating a reactive counter component using `createSignal` and rendering it to the DOM. The `console.log` statement shows that the component body executes only once. ```tsx import { createSignal } from "solid-js"; import { render } from "solid-js/web"; function Counter() { const [count, setCount] = createSignal(0); const doubleCount = () => count() * 2; console.log("The body of the function runs once..."); return ( <> ); } render(Counter, document.getElementById("app")!); ``` -------------------------------- ### Create a Basic Custom Element with Solid Source: https://github.com/solidjs/solid/blob/main/packages/solid-element/README.md Use the `customElement` function to define a new web component with a tag name, default props, and a Solid template function. ```jsx import { customElement } from 'solid-element'; customElement('my-component', {someProp: 'one', otherProp: 'two'}, (props, { element }) => { // ... Solid code }) ``` -------------------------------- ### Create a Reactive Counter Component Source: https://github.com/solidjs/solid/blob/main/packages/solid/README.md Demonstrates a basic component using createSignal for state management and JSX for rendering. ```jsx import { render } from "solid-js/web"; import { createSignal } from "solid-js"; // A component is just a function that (optionally) accepts properties and returns a DOM node const Counter = props => { // Create a piece of reactive state, giving us a accessor, count(), and a setter, setCount() const [count, setCount] = createSignal(props.startingCount || 1); // The increment function calls the setter const increment = () => setCount(count() + 1); console.log( "The body of the function runs once, like you'd expect from calling any other function, so you only ever see this console log once." ); // JSX allows us to write HTML within our JavaScript function and include dynamic expressions using the { } syntax // The only part of this that will ever rerender is the count() text. return ( ); }; // The render function mounts a component onto your page render(() => , document.getElementById("app")); ``` -------------------------------- ### Configure Babel for Universal Rendering Source: https://github.com/solidjs/solid/blob/main/packages/solid/universal/README.md Set the moduleName and generate options in babel-preset-solid to target a custom renderer. ```json { "presets": [ [ "babel-preset-solid", { "moduleName": "solid-custom-dom", "generate": "universal" } ] ] } ``` -------------------------------- ### Consume a Custom Renderer Source: https://github.com/solidjs/solid/blob/main/packages/solid/universal/README.md Import the render function from the custom renderer package to mount the application. ```js import { render } from "solid-custom-dom"; function App() { // the skies the limits } render(() => , mountNode) ``` -------------------------------- ### Configure Vite for Universal Rendering Source: https://github.com/solidjs/solid/blob/main/packages/solid/universal/README.md Use the vite-plugin-solid configuration to specify the custom renderer module. ```js import { defineConfig } from 'vite'; import solidPlugin from 'vite-plugin-solid'; export default defineConfig({ plugins: [solidPlugin({ solid: { moduleName: "solid-custom-dom", generate: 'universal' } })] }); ``` -------------------------------- ### Implement a Custom Renderer Source: https://github.com/solidjs/solid/blob/main/packages/solid/universal/README.md Define the renderer methods using createRenderer and export Solid control flow components. ```js // example custom dom renderer import { createRenderer } from "solid-js/universal"; const PROPERTIES = new Set(["className", "textContent"]); export const { render, effect, memo, createComponent, createElement, createTextNode, insertNode, insert, spread, setProp, mergeProps, use } = createRenderer({ createElement(string) { return document.createElement(string); }, createTextNode(value) { return document.createTextNode(value); }, replaceText(textNode, value) { textNode.data = value; }, setProperty(node, name, value) { if (name === "style") Object.assign(node.style, value); else if (name.startsWith("on")) node[name.toLowerCase()] = value; else if (PROPERTIES.has(name)) node[name] = value; else node.setAttribute(name, value); }, insertNode(parent, node, anchor) { parent.insertBefore(node, anchor); }, isTextNode(node) { return node.type === 3; }, removeNode(parent, node) { parent.removeChild(node); }, getParentNode(node) { return node.parentNode; }, getFirstChild(node) { return node.firstChild; }, getNextSibling(node) { return node.nextSibling; } }); // Forward Solid control flow export { For, Show, Suspense, SuspenseList, Switch, Match, Index, ErrorBoundary } from "solid-js"; ``` -------------------------------- ### Configure SSR and Hydration Source: https://github.com/solidjs/solid/blob/main/packages/babel-preset-solid/README.md Specify generation targets and hydration settings for server-side and browser-side builds. ```json { "presets": [ ["solid", { "generate": "ssr", "hydratable": true }] ] } ``` ```json { "presets": [ ["solid", { "generate": "dom", "hydratable": true }] ] } ``` -------------------------------- ### Reactive Expressions in HyperScript Source: https://github.com/solidjs/solid/blob/main/packages/solid/h/README.md Shows how to manually wrap reactive expressions in functions when using HyperScript, contrasting with JSX. ```javascript // hyperscript h("div", { id: () => props.id }, () => firstName() + lastName()) ``` -------------------------------- ### Use callback refs Source: https://github.com/solidjs/solid/blob/main/packages/solid/html/README.md Only the callback form of refs is supported. ```js let myEl; html`
myEl = el} />`; ``` -------------------------------- ### Render static pages with solid-ssr Source: https://github.com/solidjs/solid/blob/main/packages/solid-ssr/README.md Use renderStatic to generate static HTML files from a list of page configurations. ```js renderStatic( PAGES.map(p => ({ entry: pathToServer, output: path.join(pathToPublic, `${p}.html`), url: `/${p}` })) ); ``` -------------------------------- ### SolidJS Counter Component with Explanation Source: https://github.com/solidjs/solid/blob/main/README.md An expanded version of the Counter component, explaining the role of `createSignal` for reactive state and how derived state is created. It also clarifies the JSX syntax and the targeted re-rendering of dynamic content. ```tsx import { createSignal } from "solid-js"; import { render } from "solid-js/web"; // A component is just a function that returns a DOM node function Counter() { // Create a piece of reactive state, giving us an accessor, count(), and a setter, setCount() const [count, setCount] = createSignal(0); //To create derived state, just wrap an expression in a function const doubleCount = () => count() * 2; console.log("The body of the function runs once..."); // JSX allows you to write HTML within your JavaScript function and include dynamic expressions using the { } syntax // The only part of this that will ever rerender is the doubleCount() text. return ( <> ); } // The render function mounts a component onto your page render(Counter, document.getElementById("app")!); ``` -------------------------------- ### Compose Custom Element with Multiple Mixins Source: https://github.com/solidjs/solid/blob/main/packages/solid-element/README.md Utilize `compose` and `withSolid` from `component-register` and `solid-element` respectively to create a custom element with additional behaviors. ```jsx import { register, compose } from 'component-register'; import { withSolid } from 'solid-element'; /* withSolid */ compose( register('my-component'), withSolid )((props, options) => // .... ) ``` -------------------------------- ### Shorthand for Static IDs and Classes in HyperScript Source: https://github.com/solidjs/solid/blob/main/packages/solid/h/README.md Shows the shorthand syntax available in HyperScript for defining static IDs and classes directly on the element string. ```javascript h("div#some-id.my-class") ``` -------------------------------- ### Callback Refs in HyperScript Source: https://github.com/solidjs/solid/blob/main/packages/solid/h/README.md Demonstrates the required callback form for refs when using HyperScript, as compiled assignment is not supported. ```javascript let myEl; h(div, { ref: (el) => myEl = el }); ``` -------------------------------- ### Enable Hot Module Replacement for Components Source: https://github.com/solidjs/solid/blob/main/packages/solid-element/README.md Use the `hot` method to enable Hot Module Replacement for a specific custom element, allowing components to be swapped without preserving state. ```js import { customElement, hot } from 'solid-element'; hot(module, 'my-component'); ``` -------------------------------- ### Compiled SolidJS Counter Component Source: https://github.com/solidjs/solid/blob/main/README.md Shows the compiled output of the SolidJS Counter component, illustrating how Solid's compiler transforms JSX into efficient real DOM operations using functions like `_$template`, `_$insert`, and `_$delegateEvents`. ```js import { template as _$template } from "solid-js/web"; import { delegateEvents as _$delegateEvents } from "solid-js/web"; import { insert as _$insert } from "solid-js/web"; //The compiler pulls out any static HTML const _tmpl$ = /*#__PURE__*/_$template(``, 2); const Counter = props => { const [count, setCount] = createSignal(props.startingCount || 1); const increment = () => setCount(count() + 1); console.log("The body of the function runs once . . ."); return (() => { //_el$ is a real DOM node! const _el$ = _tmpl$.cloneNode(true); _el$.firstChild; _el$.$$click = increment; //This inserts the count as a child of the button in a way that allows count to update without rerendering the whole button insert(_el$, count, null); return _el$; })(); }; render( () => createComponent(Counter, { startingCount: 2 }), document.getElementById("app") ); delegateEvents(["click"]); ``` -------------------------------- ### Register a Custom Element with Component Register Source: https://github.com/solidjs/solid/blob/main/packages/solid-element/README.md Directly use the `register` function from `component-register` to create a custom element, providing the tag name and default props. ```jsx import { register } from 'component-register'; /* register(tag, defaultProps) */ register('my-component', {someProp: 'one', otherProp: 'two'})((props, options) => // .... ) ``` -------------------------------- ### Event Handling on Components with HyperScript Source: https://github.com/solidjs/solid/blob/main/packages/solid/h/README.md Highlights the requirement for explicit event handlers when using HyperScript with component props, unlike JSX. ```javascript // good h(Button, { onClick: (e) => console.log("Hi")}); // bad h(Button, { onClick: () => console.log("Hi")}) ``` -------------------------------- ### Wrap reactive expressions Source: https://github.com/solidjs/solid/blob/main/packages/solid/html/README.md Reactive expressions must be manually wrapped in functions to maintain reactivity. ```js // jsx
{firstName() + lastName()}
// hyperscript html`
props.id}>${() => firstName() + lastName()}
` ``` -------------------------------- ### Handle component events Source: https://github.com/solidjs/solid/blob/main/packages/solid/html/README.md Events on components require explicit arguments to prevent automatic getter wrapping. ```js // good html`<${Button} onClick=${(e) => console.log("Hi")} />`; // bad html`<${Button} onClick=${() => console.log("Hi")} />`; ``` -------------------------------- ### Disable Shadow DOM for Custom Element Source: https://github.com/solidjs/solid/blob/main/packages/solid-element/README.md Call `noShadowDOM()` within the Solid template function to disable the default Shadow DOM behavior for style isolation. ```jsx import { customElement, noShadowDOM } from 'solid-element'; customElement('my-component', {someProp: 'one', otherProp: 'two'}, (props, { element }) => { noShadowDOM(); // ... Solid code }) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.