Packages
corex
0.1.0-alpha.27
0.1.2
0.1.1
0.1.0
0.1.0-rc.1
0.1.0-rc.0
0.1.0-beta.5
0.1.0-beta.4
0.1.0-beta.3
0.1.0-beta.2
0.1.0-beta.1
0.1.0-alpha.33
0.1.0-alpha.32
0.1.0-alpha.31
0.1.0-alpha.30
0.1.0-alpha.29
0.1.0-alpha.28
0.1.0-alpha.27
0.1.0-alpha.26
0.1.0-alpha.25
0.1.0-alpha.24
0.1.0-alpha.23
0.1.0-alpha.22
0.1.0-alpha.21
0.1.0-alpha.20
0.1.0-alpha.19
0.1.0-alpha.18
0.1.0-alpha.17
0.1.0-alpha.16
0.1.0-alpha.15
0.1.0-alpha.14
0.1.0-alpha.13
0.1.0-alpha.12
0.1.0-alpha.11
0.1.0-alpha.10
0.1.0-alpha.9
0.1.0-alpha.8
0.1.0-alpha.7
0.1.0-alpha.6
0.1.0-alpha.5
0.1.0-alpha.4
0.1.0-alpha.3
0.1.0-alpha.2
0.1.0-alpha.1
Accessible and unstyled UI components library written in Elixir and TypeScript that integrates Zag.js state machines into the Phoenix Framework.
Current section
Files
Jump to
Current section
Files
lib/components/tree_view/connect.ex
defmodule Corex.TreeView.Connect do
@moduledoc false
alias Corex.TreeView.Anatomy.{Props, Root, Label, Item, Branch}
import Corex.Helpers, only: [validate_value!: 1]
defp data_attr(true), do: ""
defp data_attr(false), do: nil
defp data_attr(nil), do: nil
defp depth_style(index_path) when is_list(index_path), do: "--depth: #{length(index_path)}"
defp depth_style(_), do: "--depth: 0"
@spec props(Props.t()) :: map()
def props(assigns) do
%{
"id" => assigns.id,
"data-redirect" => data_attr(assigns.redirect),
"data-default-expanded-value" =>
if assigns.controlled do
nil
else
Enum.join(validate_value!(assigns.expanded_value), ",")
end,
"data-expanded-value" =>
if assigns.controlled do
Enum.join(validate_value!(assigns.expanded_value), ",")
else
nil
end,
"data-default-selected-value" =>
if assigns.controlled do
nil
else
Enum.join(validate_value!(assigns.value), ",")
end,
"data-selected-value" =>
if assigns.controlled do
Enum.join(validate_value!(assigns.value), ",")
else
nil
end,
"data-controlled" => data_attr(assigns.controlled),
"data-selection-mode" => assigns.selection_mode,
"data-dir" => assigns.dir,
"data-on-selection-change" => assigns.on_selection_change,
"data-on-expanded-change" => assigns.on_expanded_change
}
end
@spec root(Root.t()) :: map()
def root(assigns) do
%{
"data-scope" => "tree-view",
"data-part" => "root",
"dir" => assigns.dir,
"id" => "tree-view:#{assigns.id}"
}
end
@spec label(Label.t()) :: map()
def label(assigns) do
%{
"data-scope" => "tree-view",
"data-part" => "label",
"dir" => assigns.dir,
"id" => "tree-view:#{assigns.id}:label"
}
end
@spec tree(Props.t()) :: map()
def tree(assigns) do
%{
"data-scope" => "tree-view",
"data-part" => "tree",
"dir" => assigns.dir,
"id" => "tree-view:#{assigns.id}:tree"
}
end
@spec item(Item.t()) :: map()
def item(assigns) do
base = %{
"data-scope" => "tree-view",
"data-part" => "item",
"data-value" => assigns.value,
"data-path" => encode_index_path(assigns.index_path),
"data-disabled" => data_attr(assigns.disabled),
"dir" => assigns.dir,
"id" => "tree-view:#{assigns.id}:item:#{assigns.value}",
"style" => depth_style(assigns.index_path)
}
base = if Map.get(assigns, :name), do: Map.put(base, "data-name", assigns.name), else: base
base = if assigns.redirect == false, do: Map.put(base, "data-redirect", "false"), else: base
base = if assigns.new_tab, do: Map.put(base, "data-new-tab", ""), else: base
base = if Map.get(assigns, :selected), do: Map.put(base, "data-selected", ""), else: base
if Map.get(assigns, :focused), do: Map.put(base, "data-focus", ""), else: base
end
@spec branch(Branch.t()) :: map()
def branch(assigns) do
state = if assigns.expanded, do: "open", else: "closed"
base = %{
"data-scope" => "tree-view",
"data-part" => "branch",
"data-value" => assigns.value,
"data-path" => encode_index_path(assigns.index_path),
"data-state" => state,
"data-disabled" => data_attr(assigns.disabled),
"dir" => assigns.dir,
"id" => "tree-view:#{assigns.id}:branch:#{assigns.value}"
}
base = if Map.get(assigns, :name), do: Map.put(base, "data-name", assigns.name), else: base
base = if Map.get(assigns, :selected), do: Map.put(base, "data-selected", ""), else: base
if Map.get(assigns, :focused), do: Map.put(base, "data-focus", ""), else: base
end
@spec branch_trigger(Branch.t()) :: map()
def branch_trigger(assigns) do
state = if assigns.expanded, do: "open", else: "closed"
base = %{
"data-scope" => "tree-view",
"data-part" => "branch-control",
"data-value" => assigns.value,
"data-path" => encode_index_path(assigns.index_path),
"data-state" => state,
"data-disabled" => data_attr(assigns.disabled),
"dir" => assigns.dir,
"style" => depth_style(assigns.index_path)
}
base = if Map.get(assigns, :selected), do: Map.put(base, "data-selected", ""), else: base
if Map.get(assigns, :focused), do: Map.put(base, "data-focus", ""), else: base
end
@spec branch_content(Branch.t()) :: map()
def branch_content(assigns) do
state = if assigns.expanded, do: "open", else: "closed"
base = %{
"data-scope" => "tree-view",
"data-part" => "branch-content",
"data-value" => assigns.value,
"data-path" => encode_index_path(assigns.index_path),
"data-state" => state,
"dir" => assigns.dir
}
if assigns.expanded, do: base, else: Map.put(base, "hidden", "")
end
@spec branch_indicator(Branch.t()) :: map()
def branch_indicator(assigns) do
state = if assigns.expanded, do: "open", else: "closed"
%{
"data-scope" => "tree-view",
"data-part" => "branch-indicator",
"data-value" => assigns.value,
"data-path" => encode_index_path(assigns.index_path),
"data-state" => state,
"data-disabled" => data_attr(assigns.disabled),
"dir" => assigns.dir
}
end
@spec branch_text(Branch.t()) :: map()
def branch_text(assigns) do
%{
"data-scope" => "tree-view",
"data-part" => "branch-text",
"data-value" => assigns.value,
"data-path" => encode_index_path(assigns.index_path),
"dir" => assigns.dir
}
end
@spec branch_indent_guide(Branch.t()) :: map()
def branch_indent_guide(assigns) do
%{
"data-scope" => "tree-view",
"data-part" => "branch-indent-guide",
"data-value" => assigns.value,
"data-path" => encode_index_path(assigns.index_path),
"dir" => assigns.dir
}
end
defp encode_index_path(nil), do: nil
defp encode_index_path([]), do: nil
defp encode_index_path(path) when is_list(path), do: Enum.join(path, "/")
end