Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support specifying column ratios in Kino.Layout.grid #459

Merged

Conversation

elepedus
Copy link
Contributor

This PR allows the behaviour of the grid to be controlled using grid-template-columns

Example usage
Screenshot 2024-07-23 at 13 12 37

Requires Livebook PR #2718

@jonatanklosko
Copy link
Member

Hey @elepedus, is there a use case you run into, and if so, which grid feature do you want to use (fixed values, fractions)?

Ideally I would like to avoid tying options so directly to the grid implementation details, so I'm trying to get more context :)

@elepedus
Copy link
Contributor Author

Hi @jonatanklosko, I'd like to use it almost exactly as in the first example, to build a tug-of-war game. Eg:

defmodule Game do
  use GenServer
  def start_link(_), do: GenServer.start_link(__MODULE__, [])
  def init(_), do: {:ok, self(), {:continue, :setup}}
  def handle_continue(_,_) do
    state = %{
      bar: KinoProgressBar.new(value: 50, max: 100, style: "width: 100%; height: 100%"),
      value: 50
    }
    
    [&(&1 - 1), state.bar, &(&1 + 1)]
    |> Enum.map(fn 
      op when is_function(op) ->
          Kino.Control.button("Pull")
          |> tap(&Kino.Control.subscribe(&1,op))
      t -> t
    end)
    |> Kino.Layout.grid(columns: 3, template: "1fr 20fr 1fr")
    |> Kino.render()
    {:noreply, state}
  end
end
Kino.start_child!(Game)
Screenshot 2024-07-25 at 09 55 08

Without the ability to influence the width of the grid columns, the best I can do is a lot less compelling:
Screenshot 2024-07-25 at 09 56 39

Beyond that, I am (ab-)using Livebook as a rapid prototyping platform, and being able to control the grid template would give me more power to build e.g navbars etc

@elepedus
Copy link
Contributor Author

P.S.: here's a working example 🙂

https://elepedus-livebook-pr-2718.hf.space/apps/lb-tow

@jonatanklosko
Copy link
Member

Sorry for the late reply.

One more reason why I want to avoid direct CSS is to prevent from CSS injection. As opposed to other custom elements, the grid is not in an iframe, so if we simply interpolate the CSS someone could make the grid take the whole screen and whatnot. It is not necessarily critical, but I would prefer to not worry about those cases.

Instead of the template, I think we could support columns: {1, 10, 1}, meaning 3 columns in these proportions. Would that work for your case?

@elepedus
Copy link
Contributor Author

No worries, yeah, the ability to just pass ratios would be fine. Presumably we can interpolate the integer values into the grid-template-columns directive, which will achieve the same thing without opening up arbitrary CSS injection.

We would need to make sure we're not overly strict in validation, since the numbers don't strictly have to add up to a 12-column grid. E.g: in my example, I use 1 - 20 - 1, to make sure the end columns are as tight as possible around the buttons, and the middle column expands to fill all remaining space.

@jonatanklosko
Copy link
Member

We would need to make sure we're not overly strict in validation

Oh, I meant proportions, not n-based grid, so no need for validations. In Livebook we can do this:

cond do
  is_integer(columns) ->
    "repeat(#{columns}, minmax(0, 1fr))"

  is_tuple(columns) and Enum.all?(columns, &is_integer/1) ->
    columns
    |> Tuple.to_list()
    |> Enum.map_join(", ", fn n -> "minmax(0, #{n}fr)" end)

  true ->
    ""
end

If that works for you, feel free to update both PRs in this direction :)

@elepedus elepedus force-pushed the configurable_grid_template_columns branch from ce837ac to 4a288c3 Compare August 4, 2024 20:42
@elepedus
Copy link
Contributor Author

elepedus commented Aug 4, 2024

Ahh.. I had misunderstood your initial suggestion. It's actually much more elegant than what I had in mind! Thanks for taking the time to help me out with this!

I've updated both PRs accordingly! :)

lib/kino/layout.ex Outdated Show resolved Hide resolved
@jonatanklosko jonatanklosko changed the title Add :template keyword argument to Kino.Layout.Grid to control flexbox grid behaviour Support specifying column ratios in Kino.Layout.grid Aug 5, 2024
@jonatanklosko jonatanklosko merged commit 3b2153c into livebook-dev:main Aug 5, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants