Skip to content

Inputs

Reactive inputs, adapted from @observablehq/inputs.

Want More?

Currently Genji only exports a subset of inputs in @observablehq/inputs, you can open a PR if you want more.

fromElement(element)

Returns a input that produce new value whenever the given element emits an input event, with the given element’s current value.

js
custom = Inputs.fromElement(document.createElement("input"));
js
`custom is ${custom}.`;

button(content[, options])

Returns a button input. For more information, refer to the @observablehq/inputs#button documentation.

js
count = Inputs.button("Add", {
  value: 1,
  reduce: (value) => value + 1,
});
js
call(() => {
  const div = document.createElement("div");
  const span = () => `
    <span style="
      display:inline-block;
      width: 10px;
      height: 30px;
      background: black;
    ">
    </span>`;
  div.innerHTML = Array.from({ length: count }, span).join("");
  return div;
});

color(options)

Returns a color input. For more information, refer to the @observablehq/inputs#color documentation.

js
fill = Inputs.color({ label: "fill", value: "#000" });
js
call(() => {
  const div = document.createElement("div");
  div.style.width = "100px";
  div.style.height = "100px";
  div.style.background = fill;
  return div;
});

range(extent[, options])

Returns a range input. For more information, refer to the @observablehq/inputs#range documentation.

js
size = Inputs.range([0, 300], { label: "size", value: 100, step: 1 });
js
call(() => {
  const div = document.createElement("div");
  div.style.width = size + "px";
  div.style.height = "100px";
  div.style.background = "black";
  return div;
});

select(data[, options])

Returns a select input. For more information, refer to the @observablehq/inputs#select documentation.

js
type = Inputs.select(["S", "M", "L"], { label: "type" });
js
call(() => {
  const div = document.createElement("div");
  const size = type === "S" ? 50 : type === "M" ? 100 : 150;
  div.style.width = size + "px";
  div.style.height = size + "px";
  div.style.background = "black";
  return div;
});

toggle(options)

Returns a toggle input. For more information, refer to the @observablehq/inputs#toggle documentation.

js
mute = Inputs.toggle({ label: "Mute" });
js
mute;

radio(data[, options])

Returns a radio input. For more information, refer to the @observablehq/inputs#radio documentation.

js
color = Inputs.radio(["red", "green", "blue"], {
  label: "Color",
  value: "red",
});
js
call(() => {
  const div = document.createElement("div");
  div.style.width = 100 + "px";
  div.style.height = 100 + "px";
  div.style.background = color;
  return div;
});

table(data[, options])

Returns a table input. For more information, refer to the @observablehq/inputs#table documentation.

js
data = Inputs.table([
  { genre: "Sports", sold: 275 },
  { genre: "Strategy", sold: 115 },
  { genre: "Action", sold: 120 },
  { genre: "Shooter", sold: 350 },
  { genre: "Other", sold: 150 },
]);
js
data;

Returns a search input. For more information, refer to the @observablehq/inputs#search documentation.

js
state = Inputs.search(
  ["Alabama", "Alaska", "Arizona", "Arkansas", "California"],
  { label: "State" }
);
js
state;

text(options)

Returns a text input. For more information, refer to the @observablehq/inputs#text documentation.

js
name = Inputs.text({ label: "Name", placeholder: "Enter your name" });
js
`You name is ${name}.`;

Released under the MIT License.