pink/hook
Types
App
is a record that is returned by the app
hook.
pub type App {
App(exit: fn() -> Nil)
}
Constructors
-
App(exit: fn() -> Nil)
FocusManager
is a record that is returned by the focus_manager
hook.
pub type FocusManager {
FocusManager(
enable_focus: fn() -> Nil,
disable_focus: fn() -> Nil,
focus_next: fn() -> Nil,
focus_previous: fn() -> Nil,
focus: fn(String) -> Nil,
)
}
Constructors
-
FocusManager( enable_focus: fn() -> Nil, disable_focus: fn() -> Nil, focus_next: fn() -> Nil, focus_previous: fn() -> Nil, focus: fn(String) -> Nil, )
State
is a record that is returned by the state
hook.
pub type State(a) {
State(
value: a,
set: fn(a) -> Nil,
set_with: fn(fn(a) -> a) -> Nil,
)
}
Constructors
-
State( value: a, set: fn(a) -> Nil, set_with: fn(fn(a) -> a) -> Nil, )
Stderr
is a record that is returned by the stderr
hook.
pub type Stderr {
Stderr(stderr: Dynamic, write: fn(String) -> Nil)
}
Constructors
-
Stderr(stderr: Dynamic, write: fn(String) -> Nil)
Stdin
is a record that is returned by the stdin
hook.
pub type Stdin {
Stdin(
stdin: Dynamic,
is_raw_mode_supported: Bool,
set_raw_mode: fn(Bool) -> Nil,
)
}
Constructors
-
Stdin( stdin: Dynamic, is_raw_mode_supported: Bool, set_raw_mode: fn(Bool) -> Nil, )
Functions
pub fn app() -> App
app
is a hook which exposes a function to manually exit the app (unmount).
pub fn effect(
callback: fn() -> Nil,
dependencies: List(a),
) -> Nil
effect
is a hook which allows you to perform side effects in function components.
This is a React hook and not a Ink hook.
Read more about it on react.dev docs
This hook is here for convenience, so you don’t have to create the bindings yourself
If you want to run a cleanup function when the component unmounts, use effect_clean
instead
pub fn effect_clean(
callback: fn() -> fn() -> Nil,
dependencies: List(a),
) -> Nil
This is the same as effect
, but it also allows you to run a cleanup function when the component unmounts
pub fn focus(options: FocusOptions) -> Focus
Component that uses useFocus hook becomes “focusable” to Ink, so when user presses Tab, Ink will switch focus to this component. If there are multiple components that execute useFocus hook, focus will be given to them in the order that these components are rendered in.
This hook returns a record with is_focused
boolean field, which determines if this component is focused or not
pub fn focus_manager() -> FocusManager
This hook exposes methods to enable or disable focus management for all components or manually switch focus to next or previous components.
The FocusManager
record contains the following fields:
enable_focus
- Enable focus management for all components Note: You don’t need to call this method manually, unless you’ve disabled focus management. Focus management is enabled by defaultdisable_focus
- Disable focus management for all components. Currently active component (if there’s one) will lose its focus.focus_next
- Switch focus to the next focusable component. If there’s no active component right now, focus will be given to the first focusable component. If active component is the last in the list of focusable components, focus will be switched to the first active component. Note: Ink calls this method when user presses Tab.focus_previous
- Switch focus to the previous focusable component. If there’s no active component right now, focus will be given to the last focusable component. If active component is the first in the list of focusable components, focus will be switched to the last active component. Note: Ink calls this method when user presses Shift+Tab.focus
- Switch focus to the component with the specified id. If there’s no component with that ID, focus will be given to the next focusable component.
pub fn input(
callback: fn(String, List(Key)) -> Nil,
is_active: Bool,
) -> Nil
This hook is used for handling user input.
It’s a more convenient alternative to using stdin
and listening to data events.
The callback you pass to input
is called for each character when user enters any input.
However, if user pastes text and it’s more than one character, the callback will be called only once and the whole string will be passed as input.
The is_active
option is used to enable or disable capturing of user input.
Useful when there are multiple input
hooks used at once to avoid handling the same input several times
Examples
component(fn() {
hook.input(fn(input, keys) {
case input, keys {
"q", _ -> // Exit program
_, [LeftArrow] -> // Left arrow key pressed
}
}, True)
})
pub fn state(initial: a) -> State(a)
state
is a hook which allows you to create a stateful value.
This is a React hook and not a Ink hook.
Read more about it on react.dev docs
This hook is here for convenience, so you don’t have to create the bindings yourself
state
returns a record with the following fields:
value
- The current state valueset
- A function to update the state value.set_with
- A function to update the state value. It accepts a function which receives the current state value and returns a new state value.
pub fn stderr() -> Stderr
stderr
is a hook which exposes stderr stream.
The Stderr
record contains the following fields:
stderr
- process.stderrwrite
- Write any string to stderr, while preserving Ink’s output. It’s useful when you want to display some external information outside of Ink’s rendering and ensure there’s no conflict between the two. It’s similar tostatic
, except it can’t accept components, it only works with strings
pub fn stdin() -> Stdin
stdin
is a hook which exposes stdin stream
The Stdin
record contains the following fields:
stdin
- process.stdin. Useful if your app needs to handle user inputis_raw_mode_supported
- A boolean flag determining if the current stdin supportsset_raw_mode
. A component usingset_raw_mode
might want to useis_raw_mode_supported
to nicely fall back in environments where raw mode is not supportedset_raw_mode
- See setRawMode. Ink exposes this function to be able to handle Ctrl+C, that’s why you should use Ink’sset_raw_mode
instead ofprocess.stdin.setRawMode
pub fn stdout() -> Stdout
stdout
is a hook which exposes stdout stream, where Ink renders your app.
The Stdout
record contains the following fields:
stdout
- process.stdoutwrite
- Write any string to stdout, while preserving Ink’s output. It’s useful when you want to display some external information outside of Ink’s rendering and ensure there’s no conflict between the two. It’s similar tostatic
, except it can’t accept components, it only works with strings