Creating components
First, start the compiler using npm run dev. This will watch for any changes and compile the updated theme into the dist/ folder.
Each time the compiler performs a build it'll output a file listing showing which dist files were updated and their size.
The compiler will only write output files to dist/ when they have changed - if there were any unchangd files the compiler will show that on the last line.
All of the TS code for your theme lives inside the src/ folder. Inside this is also the special folder src/components, which is where you define components.
To create your first component, create a new file src/components/TabContainer.ts:
src/components/GreeterComponent.tstsimport { Component } from '@eastsideco/salvo-ts';import { ThemeComponent } from '../theme/ThemeComponent';@Componentexport class GreeterComponent extends ThemeComponent {}
src/components/GreeterComponent.tstsimport { Component } from '@eastsideco/salvo-ts';import { ThemeComponent } from '../theme/ThemeComponent';@Componentexport class GreeterComponent extends ThemeComponent {}
Let's break this down:
@Component- This is a decorator which marks this class as a component. It's required on every component.export class GreeterComponent- This class represents the component we're making. We'll put the component's behavior and logic inside this class.extends ThemeComponent- This is the default base component in your theme. Anything you add to ThemeComponent will appear in all of your components. All of the components you create should extend ThemeComponent.
Using components
To use the new component in your theme, just add a HTML tag with a matching name - in this case <greeter-component>. Add this to static/sections/product-main.liquid (or your choice of snippet/section):
static/layout/theme.liquidhtml<greeter-component></greeter-component>
static/layout/theme.liquidhtml<greeter-component></greeter-component>
Component logic
By itself this component won't do anything, so let's add some logic using the init() hook, which runs when the component has been initialized and is present on the page:
src/components/GreeterComponent.tsts@Componentexport class GreeterComponent extends ThemeComponent {init() {this.$log.info('Hello from GreeterComponent!');}}
src/components/GreeterComponent.tsts@Componentexport class GreeterComponent extends ThemeComponent {init() {this.$log.info('Hello from GreeterComponent!');}}
Open your theme preview and go to the product page (or the page where you added the tag) and you should see Hello from GreeterComponent! in your devtools console.
Rules for components
When creating your own components in the future, it's important to stick to the following rules, or your components won't load correctly:
- Each
.tsfile incomponents/must export exactly ONE component.- If you have multiple components, give each one it's own file.
- Each
.tsfile incomponents/needs to be named the same as the component it exports.- For instance, a
TabContainer.tsshould export a class namedTabContainer.
- For instance, a
- If you are using a component from outside it's own file, you must
importit. This includes finding a component usingquerySelectororgetElementById.- If you don't do this then it's possible the component won't be loaded in the browser when you try to modify it or call a function on it.
- See the cross-component manipulation example.
- Components may be organized in subfolders, but the name of the component must contain the subfolder as a prefix.
- For example,
components/Product/ProductFormis valid butcomponents/Product/FeaturesBoxis not. This helps find components more easily.
- For example,
Next steps
Follow the rest of this tutorial to find out how to work with other assets in your themes, then check out the handbook to learn features which will let you use components more effectively, including component templates, and events.