Plushie.Route (Plushie v0.6.0)

Copy Markdown View Source

Client-side routing for multi-view apps. Pure data structure maintaining a navigation stack of {path, params} entries.

The stack is last-in-first-out. push/3 adds a new entry on top; pop/1 removes the top entry (never pops the last one). current/1 and params/1 read from the top of the stack.

Example

route = Plushie.Route.new(:home)
route = Plushie.Route.push(route, :settings, %{tab: "general"})
Plushie.Route.current(route)
#=> :settings
Plushie.Route.params(route)
#=> %{tab: "general"}
route = Plushie.Route.pop(route)
Plushie.Route.current(route)
#=> :home

Summary

Types

A navigation stack entry: {path, params}.

t()

Functions

Returns true if there is more than one entry on the stack.

Returns the current (top) path.

Returns a list of all paths in the stack, most recent first.

Creates a new route with initial_path at the bottom of the stack.

Returns the params associated with the current (top) path.

Pops the top entry from the stack. Returns the route unchanged if only one entry remains (the root is never popped).

Pushes a new path (with optional params) onto the navigation stack.

Replaces the top entry with a new path and params.

Types

entry()

@type entry() :: {term(), map()}

A navigation stack entry: {path, params}.

t()

@type t() :: %Plushie.Route{stack: [entry()]}

Functions

can_go_back?(route)

@spec can_go_back?(route :: t()) :: boolean()

Returns true if there is more than one entry on the stack.

current(route)

@spec current(route :: t()) :: term()

Returns the current (top) path.

history(route)

@spec history(route :: t()) :: [term()]

Returns a list of all paths in the stack, most recent first.

new(initial_path, params \\ %{})

@spec new(initial_path :: term(), params :: map()) :: t()

Creates a new route with initial_path at the bottom of the stack.

params defaults to an empty map.

params(route)

@spec params(route :: t()) :: map()

Returns the params associated with the current (top) path.

pop(route)

@spec pop(route :: t()) :: t()

Pops the top entry from the stack. Returns the route unchanged if only one entry remains (the root is never popped).

push(route, path, params \\ %{})

@spec push(route :: t(), path :: term(), params :: map()) :: t()

Pushes a new path (with optional params) onto the navigation stack.

replace_top(route, path, params \\ %{})

@spec replace_top(route :: t(), path :: term(), params :: map()) :: t()

Replaces the top entry with a new path and params.