pink/hook

Functions

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 the focus 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 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)
})
Search Document