Add support for specifying column ratios in grid output (#2718)

Co-authored-by: Jonatan Kłosko <jonatanklosko@gmail.com>
This commit is contained in:
Ed Lepedus 2024-08-05 12:36:53 +01:00 committed by GitHub
parent b24f4c322e
commit eefeb7aa6c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 16 additions and 2 deletions

View file

@ -263,7 +263,7 @@ defprotocol Livebook.Runtime do
@type grid_output :: %{
type: :grid,
outputs: list(t()),
columns: pos_integer(),
columns: pos_integer() | tuple(),
gap: non_neg_integer(),
boxed: boolean()
}

View file

@ -30,7 +30,7 @@ defmodule LivebookWeb.Output.GridComponent do
<div
id={"#{@id}-grid"}
class="grid grid-cols-2 w-full"
style={"grid-template-columns: repeat(#{@columns}, minmax(0, 1fr)); gap: #{@gap}px"}
style={"grid-template-columns: #{make_template(@columns)}; gap: #{@gap}px"}
phx-update="stream"
>
<div :for={{dom_id, output} <- @streams.outputs} id={dom_id}>
@ -48,4 +48,18 @@ defmodule LivebookWeb.Output.GridComponent do
</div>
"""
end
defp make_template(columns) when is_tuple(columns) do
columns = Tuple.to_list(columns)
if Enum.all?(columns, &is_integer/1) do
Enum.map_join(columns, " ", fn n -> "minmax(0, #{n}fr)" end)
else
""
end
end
defp make_template(columns) when is_integer(columns), do: "repeat(#{columns}, minmax(0, 1fr))"
defp make_template(_columns), do: ""
end