Controllers: ReactiveVariableController
ReactiveVariableController
is a convenience wrapper around apollo reactive variables.
Apollo Elements controllers are not limited to Lit. Use them with any object that implements the ReactiveControllerHost
interface. See ControllerHostMixin
for an example.
import { customElement } from 'lit/decorators.js';
import { html, LitElement } from 'lit';
import { ReactiveVariableController } from '@apollo-elements/core';
import { locationVar } from './router.js';
import style from './profile-page.css.js';
@customElement('profile-page')
class ProfilePage extends LitElement {
static styles = style;
router = new ReactiveVariableController(this, locationVar);
render() {
const { username } = this.router.value?.groups ?? {}
return html`
<section ?hidden=${!username}>
<h2>User ${username}</h2>
</section>
`;
}
}
import { css } from 'lit';
export default css`
:host { display: block; }
[hidden] { display: none !important; }
`;
<script type="module" src="./profile-page.js"></script>
<profile-home></profile-home>
import 'urlpattern-polyfill';
import { installRouter } from 'pwa-helpers/router';
import { makeVar } from '@apollo/client/core';
const pattern = new URLPattern({ pathname: '/users/:username' });
const getLocation = (loc = window.location) => ({ ...loc,
groups: pattern.exec(loc.pathname)?.pathname?.groups
})
export const locationVar = makeVar(getLocation());
installRouter(loc => locationVar(getLocation(loc)));
Fired when a reactive variable's value changes
Wraps a reactive variable in a reactive controller for easy integration.
Examples
Properties
value
T
The latest value of the reactive variable
Methods
set
Convenient wrapper around reactiveVariable(newVal)
Parameters
x
T
Private API
Private Properties
#onNextChange
Private Methods
hostUpdate
Returns
unknown
Exports
import {
VariableChangeEvent,
ReactiveVariableController
} from '@apollo-elements/core/reactive-variable-controller';