Current section

Files

Jump to
react_gleam src react_gleam@router.erl
Raw

src/react_gleam@router.erl

-module(react_gleam@router).
-compile([no_auto_import, nowarn_unused_vars]).
-export([push/1, back/0, use_url/0]).
-export_type([location/0, dom_event/0]).
-type location() :: {location, list(binary()), binary(), binary()}.
-type dom_event() :: any().
-spec push(binary()) -> nil.
push(Field@0) ->
'../ffi.mjs':'pushState'(Field@0).
-spec back() -> nil.
back() ->
'../ffi.mjs':'popState'().
-spec use_url() -> location().
use_url() ->
Location = '..@ffi.mjs':'currentLocation'(),
{Current_location, Set_current_location} = '..@ffi.mjs':'useState'(
fun(_) -> Location end
),
'..@ffi.mjs':'useEffect0'(
fun() ->
Location_change_callback = fun() ->
New_location = '..@ffi.mjs':'currentLocation'(),
Set_current_location(fun(_) -> New_location end)
end,
Location_pop_callback = fun() ->
'..@ffi.mjs':'dispatchEvent'(
'..@ffi.mjs':'newEvent'(<<"locationchange"/utf8>>)
)
end,
'..@ffi.mjs':'addEventListener'(
<<"locationchange"/utf8>>,
Location_change_callback
),
'..@ffi.mjs':'addEventListener'(
<<"popstate"/utf8>>,
Location_pop_callback
),
{some,
fun() ->
'..@ffi.mjs':'removeEventListener'(
<<"locationchange"/utf8>>,
Location_change_callback
),
'..@ffi.mjs':'removeEventListener'(
<<"popstate"/utf8>>,
Location_pop_callback
)
end}
end
),
Current_location.