Skip to content

Commit

Permalink
Add Router.link ~alias for an additional active check
Browse files Browse the repository at this point in the history
  • Loading branch information
rizo committed Jun 19, 2024
1 parent 7eaf013 commit d8893ab
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
18 changes: 11 additions & 7 deletions src/helix/Helix.mli
Original file line number Diff line number Diff line change
Expand Up @@ -335,20 +335,24 @@ module Router : sig
?active:Html.attr ->
?inactive:Html.attr ->
?exact:bool ->
?alias:(unit -> 'alias, 'alias, 'alias) path ->
?up:int ->
t ->
('view, 'link, Html.attr) path ->
'link
(** [link ?absolute ?active ?exact router path vars...] is an HTML [href]
attribute that binds a link described by [path] and any [vars] contained
in it (or none, if it's a const only path). A link relative to [router]
will be created, unless [absolute] is [true].
(** [link ?absolute ?active ?exact ?alias router path vars...] is an HTML
[href] attribute that binds a link described by [path] and any [vars]
contained in it (or none, if it's a const only path). A link relative to
[router] will be created, unless [absolute] is [true].
If [active] attribute is provided, in addition to binding [href], [active]
will be bound in case the current path is active, otherwise [inactive] is
bound (if provided). By default a path is considered active if it is a
prefix of the current path, unless [exact] is [true], in which case the
path is only considered active when it is equal to the current path. *)
bound (if provided).
By default a path is considered active if it is a prefix of the current
path, unless [exact] is [true], in which case the path is only considered
active when it is equal to the current path. Additionally, the path is
considered active if it is equal to [alias]. *)

val route : ('view, 'link, Html.elem) path -> 'view -> route
(** Create a route by assigning a path to a view. *)
Expand Down
12 changes: 8 additions & 4 deletions src/helix/Router.ml
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,15 @@ let pick_qpath segments =
segments

let link ?(absolute = false) ?(active = Html.Attr.empty)
?(inactive = Html.Attr.empty) ?(exact = false) ?(up = 0) (router : t) path0
=
?(inactive = Html.Attr.empty) ?(exact = false) ?alias ?(up = 0) (router : t)
path0 =
let alias = Option.map string_of_path alias in
let check_is_active link curr =
if exact then String.equal link curr
else String.starts_with ~prefix:link curr
match alias with
| Some alias -> String.equal alias curr
| None ->
if exact then String.equal link curr
else String.starts_with ~prefix:link curr
in
eval_path
(fun str_path ->
Expand Down

0 comments on commit d8893ab

Please sign in to comment.