import { Transition, HookResult, _ViewDeclaration, StateDeclaration, UIRouter, RawParams } from "@uirouter/core"; import { TemplateResult, LitElement } from "lit"; /** * Interface for components that respond to parameter changes. * * When a component implements this interface, UI-Router will call the * `uiOnParamsChanged` method whenever dynamic parameter values change * without destroying and recreating the component. * * @example * ```ts * class UserDetail extends LitElement implements UiOnParamsChanged { * uiOnParamsChanged(newParams: RawParams, trans?: Transition) { * console.log('Parameters changed:', newParams); * // React to the new userId * if (newParams.userId) { * this.loadUser(newParams.userId); * } * } * } * ``` * * @see [[TransitionOptions]] * * @category hooks */ export interface UiOnParamsChanged { /** * A UI-Router view has a Lit `Component` (see [[NormalizedLitViewDeclaration.component]]). * The `Component` may define component-level hooks which UI-Router will call at the appropriate times. * These callbacks are similar to Transition Hooks ([[IHookRegistry]]), but are only called if the view/component is currently active. * * The uiOnParamsChanged callback is called when parameter values change. * * This callback is used to respond dynamic parameter values changing. * It is called when a transition changed one or more dynamic parameter values, * and the routed component was not destroyed. * * It receives two parameters: * * - An object with (only) changed parameter values. * The keys are the parameter names and the values are the new parameter values. * - The [[Transition]] which changed the parameter values. * */ uiOnParamsChanged(newParams: RawParams, trans?: Transition): void; } /** * Interface for components that can prevent or confirm navigation away. * * When a component implements this interface, UI-Router will call the * `uiCanExit` method before navigating away from the component's state. * This can be used to prompt for confirmation or prevent navigation. * * @example * ```ts * class EditForm extends LitElement implements UiOnExit { * hasUnsavedChanges = false; * * uiCanExit(trans?: Transition): HookResult { * if (this.hasUnsavedChanges) { * return window.confirm('Discard unsaved changes?'); * } * return true; * } * } * ``` * * @see [[HookResult]] * @see [[Transition]] * * @category hooks */ export interface UiOnExit { /** * A UI-Router view has a Lit `Component` (see [[NormalizedLitViewDeclaration.component]]). * The `Component` may define component-level hooks which UI-Router will call at the appropriate times. * These callbacks are similar to Transition Hooks ([[IHookRegistry]]), but are only called if the view/component is currently active. * * The uiCanExit callback is called when the routed component's state is about to be exited. * * The callback can be used to cancel or alter the new [[Transition]] that would otherwise exit the component's state. * * This callback is used to inform a view that it is about to be exited, due to a new [[Transition]]. * The callback can ask for user confirmation, and cancel or alter the new Transition. The callback should * return a value, or a promise for a value. If a promise is returned, the new Transition waits until the * promise settles. * * Called when: * - The component is still active inside a `ui-view` * - A new Transition is about to run * - The new Transition will exit the view's state * * Called with: * - The [[Transition]] that is about to exit the component's state * * @returns a hook result which may cancel or alter the pending Transition (see [[HookResult]]) */ uiCanExit(newTransition?: Transition): HookResult; } /** * Default Resolves Types when not provided to Generic UIViewResolves * @see [[UIViewResolves]] * @category types */ export type DefaultResolvesType = Record; /** * Type alias for resolved values passed to routed components. * * @template T - The shape of the resolved values object * * @see [[UIViewInjectedProps]] * @see [[DefaultResolvesType]] * @see [[StateDeclaration.resolve]] * * @category types */ export type UIViewResolves = T; /** * Props injected into routed components by the `` element. * * These props provide access to the current transition, resolved data, * and the router instance. Components can use these to access routing * state and navigate programmatically. * * @template T - The shape of the resolved values object * * @example Using in a template function * ```ts * const UserDetail: RoutedLitTemplate = (props) => { * const { resolves, router } = props!; * return html` *

User: ${resolves?.user?.name}

* * `; * }; * ``` * * @example Using in a LitElement class * ```ts * class UserDetail extends LitElement { * _uiViewProps?: UIViewInjectedProps<{ user: User }>; * * render() { * const user = this._uiViewProps?.resolves?.user; * return html`

${user?.name}

`; * } * } * ``` * * @see [[UIRouter]] * @see [[Transition]] * * @category types */ export interface UIViewInjectedProps { /** The current transition (if one is in progress) */ transition?: Transition; /** Resolved data from state declarations */ resolves: UIViewResolves; /** The UIRouter instance for programmatic navigation */ router: UIRouter; } /** * A function that returns a Lit TemplateResult for rendering in a ``. * * This is the **simplest way** to define route components - no LitElement class needed. * The function optionally receives [[UIViewInjectedProps]] as its argument. * * @example Simple template (no props needed) * ```ts * const HomeView: RoutedLitTemplate = () => html`

Welcome Home

`; * * // Or use directly inline in a state declaration: * { name: 'home', url: '/', component: () => html`

Home

` } * ``` * * @example With route parameters * ```ts * const UserView: RoutedLitTemplate = (props) => html` *

User: ${props?.transition?.params().id}

* `; * ``` * * @example With resolved data (typed) * ```ts * interface UserResolves { user: { name: string } } * * const UserDetail: RoutedLitTemplate = (props) => html` *

${props?.resolves?.user?.name}

* `; * ``` * * @category types */ export type RoutedLitTemplate = (props: UIViewInjectedProps) => TemplateResult; /** * A template function that can be used as a view declaration. * * A {@link RoutedLitTemplate} intersected with the core view declaration * metadata (all optional, so plain template functions remain assignable). */ export type LitViewDeclarationTemplate = RoutedLitTemplate & _ViewDeclaration; /** * A LitElement class constructor that can be used in state declarations. * * The class should extend LitElement. The router delivers {@link UIViewInjectedProps} * two ways: as a constructor argument on first render, and by assigning the * `_uiViewProps` property on every render (the only channel that fires when a * `sticky` instance is reused). Constructor args are optional — argless * constructors, as is typical for custom elements, work too. The `sticky` * property can be set to `true` to reuse the same component instance across * state transitions. * * @example Constructor injection * ```ts * class UserList extends LitElement { * _uiViewProps: UIViewInjectedProps; * * constructor(props: UIViewInjectedProps) { * super(); * this._uiViewProps = props; * } * * render() { * return html`

Users

`; * } * } * * router.stateRegistry.register({ * name: 'users', * url: '/users', * component: UserList * }); * ``` * * @example Argless constructor with property injection (typical Lit) * ```ts * class UserDetail extends LitElement { * // reactive property: sticky reuse re-renders when props are reassigned * @property({ attribute: false }) _uiViewProps?: UIViewInjectedProps; * * render() { * return html`

${this._uiViewProps?.transition?.params().id}

`; * } * } * ``` * * @category types */ export interface RoutedLitElement { /** * Construct signature. The router always passes props; classes may declare * the parameter required, optional, or not at all. */ new (props: UIViewInjectedProps): LitElement & { _uiViewProps?: UIViewInjectedProps; }; /** If true, the same component instance is reused across state transitions */ sticky?: boolean; } /** * Union type for components that can be used in state declarations. * * A routed component can be either: * - A {@link RoutedLitTemplate} function that returns a TemplateResult * - A {@link RoutedLitElement} class that extends LitElement * * @category types */ export type RoutedLitComponent = RoutedLitTemplate | RoutedLitElement; /** * A LitElement class used directly as a view declaration. */ export interface LitViewDeclarationElement extends RoutedLitElement, _ViewDeclaration {} /** * A view declaration object with an explicit component property. */ export interface LitViewDeclarationObject extends _ViewDeclaration { component: RoutedLitComponent; } /** * Union type for all valid view declaration formats. * * A view can be declared as: * - An object with a `component` property ({@link LitViewDeclarationObject}) * - A LitElement class directly ({@link LitViewDeclarationElement}) * - A template function directly ({@link LitViewDeclarationTemplate}) */ export type LitViewDeclaration = LitViewDeclarationObject | LitViewDeclarationElement | LitViewDeclarationTemplate; /** * State declaration interface for Lit applications. * * Extends the core [[StateDeclaration]] with Lit-specific component support. * The `component` property accepts template functions, LitElement classes, or both. * * @example Simplest: inline template function * ```ts * { name: 'home', url: '/', component: () => html`

Home

` } * ``` * * @example Template with route parameters * ```ts * { * name: 'user', * url: '/user/:id', * component: (props) => html`

User ${props?.transition?.params().id}

` * } * ``` * * @example Template with resolved data * ```ts * { * name: 'users', * url: '/users', * component: (props) => html` *
    ${props?.resolves?.users?.map(u => html`
  • ${u.name}
  • `)}
* `, * resolve: [{ token: 'users', resolveFn: () => fetch('/api/users').then(r => r.json()) }] * } * ``` * * @example LitElement class (for complex components) * ```ts * { name: 'dashboard', url: '/dashboard', component: DashboardElement } * ``` * * @example Nested states * ```ts * const states: LitStateDeclaration[] = [ * { name: 'app', abstract: true, component: AppShell }, * { name: 'app.home', url: '/home', component: () => html`

Home

` }, * { name: 'app.users', url: '/users', component: UsersView } * ]; * ``` * * @see [[StateDeclaration]] * @see {@link RoutedLitTemplate} * @see {@link RoutedLitElement} * * @category types */ export interface LitStateDeclaration extends StateDeclaration { /** The Lit component to render for this state */ component?: LitViewDeclaration; /** * An optional object used to define multiple named views. * * Overrides the core [[StateDeclaration.views]] property so each named view * accepts any {@link LitViewDeclaration} format — a bare component or an * object with a `component` property — threading the resolves generic. * * @see {@link LitViewDeclaration} */ views?: { [key: string]: LitViewDeclaration; }; } /** * The `litViewsBuilder` registered in [[UIRouterLit.constructor]] normalizes config to this internal interface. * * @category types */ export interface NormalizedLitViewDeclaration extends _ViewDeclaration { component: RoutedLitTemplate; } //# sourceMappingURL=interface.d.ts.map