This example demonstrates how to use ignite-element with MobX, lit-html, and custom CSS for styling, following atomic design principles.
- State management with MobX, showcasing shared and isolated components.
- Dynamic styling with CSS variables for customizable and reusable styles.
- Integration with ignite-element for seamless web component creation.
Run the following command to install all necessary dependencies:
npm install
To start the development server:
npm run dev
When running the example, you'll see:
- Shared Counter Component: A counter component using a shared global state.
- Isolated Counter Component: A counter component with isolated state for each instance.
This example uses CSS variables for styling and customization. Global styles are applied using the setGlobalStyles
function, referencing theme.css
, while component-specific styles are applied using <style>
or <link>
tags in the components.
Create a reactive MobX store with decorators for state and actions:
import { action, observable, makeObservable } from "mobx";
class Counter {
@observable count = 0;
constructor() {
makeObservable(this);
}
@action increment() {
this.count += 1;
}
@action decrement() {
this.count -= 1;
}
}
const counterStore = () => new Counter();
export default counterStore;
Add global styles from theme.css
using setGlobalStyles
:
import { setGlobalStyles } from "ignite-element";
setGlobalStyles("./theme.css");
Initialize igniteCore
with shared and isolated component support:
import { igniteCore } from "ignite-element";
import counterStore from "./mobxCounterStore";
export const { isolated, shared } = igniteCore({
adapter: "mobx",
source: counterStore,
});
shared("my-counter-mobx", (state, action) => {
return html`
<div>
<div class="container">
<h3>Shared Counter (MobX)</h3>
<p>Count: ${state.count}</p>
<div class="button-group">
<button @click=${() => action({ type: "decrement" })}>-</button>
<button @click=${() => action({ type: "increment" })}>+</button>
</div>
</div>
</div>
`;
});
shared("shared-display-mobx", (state) => {
return html`
<div class="display">
<h3>Shared State Display (MobX)</h3>
<p>Shared Count: ${state.count}</p>
</div>
`;
});
isolated("another-counter-mobx", (state, action) => {
return html`
<div>
<link rel="stylesheet" href="./another-counter-mobx.css" />
<div class="container">
<h3>Isolated Counter (Custom Styled)</h3>
<p>Count: ${state.count}</p>
<div class="button-group">
<button @click=${() => action({ type: "decrement" })}>-</button>
<button @click=${() => action({ type: "increment" })}>+</button>
</div>
</div>
</div>
`;
});
Use the custom elements in your HTML file:
<my-counter-mobx></my-counter-mobx>
<shared-display-mobx></shared-display-mobx>
<another-counter-mobx></another-counter-mobx>