edemaine:solid-template-helper

v0.1.0Published 2 years ago

SolidJS Components in Blaze Templates

This package makes it easy to use (SolidJS components within Meteor Blaze templates. This makes it easy to gradually transition your Meteor project's user interface from Blaze templates to SolidJS, enabling more-fine-grained reactivity, substantially faster and more control over (re)rendering, combined template and code via JSX, and many more advanced features (higher-order components, Stores, Context, Suspense/lazy loading, Portals, Error Boundaries, SSR, etc.).

edemaine:solid-template-package is essentially a port of react-template-helper, sharing much of the code with that package.

Usage

To add the edemaine:solid-template-package to your Meteor project:

meteor add edemaine:solid-template-helper

To then use a SolidJS component, use {{> Solid component=...}} in a Blaze template, within at least one actual HTML element (not directly within a <template>). The component argument should resolve (via a template helper) to a SolidJS component function. Optionally, you can provide other arguments (via more template helpers or, more usefully, from template data) that will be passed in as props to that component; if the arguments change reactively, the component will update reactively (avoiding a complete dispose/render cycle).

Here is an example HTML template that uses a SolidJS component:

1<template name="Hello">
2  <div>
3    {{> Solid component=Hello name=name}}
4  </div>
5</template>

And here is an example of associated JavaScript code:

1import {createSignal, createEffect, onCleanup} from 'solid-js';
2
3Template.Hello.helpers({
4  Hello: () => Hello,  // note wrapped in closure to avoid getting called early
5  name: () => Session.get('name'),
6});
7
8// SolidJS component, assuming edemaine:meteor is active
9const Hello = (props) => {
10  const [color, setColor] = createSignal('inherit');
11  createEffect(() => {
12    const interval = setInterval(
13      () => setColor(`#${Math.floor(Math.random() * 0xffffff)
14                         .toString(16).padStart(6, '0')}`),
15      200);
16    onCleanup(() => clearInterval(interval));
17  });
18  return (
19    <h1 style={{color: color()}}>
20      Hello {props.name}!
21    </h1>
22  );
23};

You might also want to use the following SolidJS + Meteor tools: