Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions docs/LAYER_4_OPTIMISTIC.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,6 @@ You don't write these by hand:
drop `nil`/`false`, space-join). Conditional classes of any shape —
`if`/`unless`, list form, string concatenation, comparisons — ride
this. Untranspilable expressions demote loudly to server-rendered.
- `data-lavash-visible="field"` — from `:if={@field}`; shows/hides
via a `hidden` class.
- `data-lavash-enabled="field"` — from `disabled={not @field}`.
- `data-lavash-member="field|sel|unsel"` + `data-lavash-member-value`
— from `class={if val in @list, …}` (the ChipSet pattern). Kept as
Expand All @@ -225,11 +223,19 @@ Injection fires on **HTML tags only**. Attributes on component calls
(`<.form>`, `<.button>`) are invisible to the pipeline even when the
component passes them through — annotate those by hand (below).

Note on `:if={@field}`: those blocks ride **subtree derives** — the
block re-renders client-side in both directions — so no annotation
is injected for them at all.

### Hand-written escape hatches

These are supported public API — each exists because no idiomatic
expression can carry the information:

- `data-lavash-visible="field"` — show/hide via a `hidden` class in
**non-lavash templates** (inside lavash templates, `:if` subtree
derives and hidden-class attribute derives cover both shapes).

- `data-lavash-display="field"` — around an expression the pipeline
won't manage itself, which in practice means **async render
blocks** (a `case` over an AsyncResult) and non-lavash templates:
Expand Down
44 changes: 10 additions & 34 deletions lib/lavash/optimistic/data_attr_transformer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,18 @@ defmodule Lavash.Optimistic.DataAttrTransformer do
gets `data-lavash-bind="count"` injected when `:count` is an
optimistic state field.

### 3. Conditional visibility
### 3. Conditional visibility (manual only)

<div :if={@open}>...</div>
`:if={@open}` blocks over optimistic fields ride subtree derives —
the block is re-rendered client-side, which handles BOTH directions
(a class-based directive can't show an element `:if` removed from
the DOM), so no `data-lavash-visible` is auto-injected. The
show/hide-by-class idiom (`class={if !@open, do: "hidden"}`) rides
pattern-7 attribute derives.

gets `data-lavash-visible="open"` injected when `:open` is an
optimistic boolean field.

The show/hide-by-class idiom (element kept in the DOM) is covered
by pattern 7 instead: `class={if !@open, do: "hidden"}` (or any
conditional class expression) gets a reactive attribute derive
that recomputes the class client-side.
`data-lavash-visible="field"` remains a supported hand-written
annotation for non-lavash templates; its JS toggles a `hidden`
class.

### 4. Enabled/disabled

Expand Down Expand Up @@ -303,7 +304,6 @@ defmodule Lavash.Optimistic.DataAttrTransformer do
attrs
|> maybe_inject_form_input(name, metadata)
|> maybe_inject_state_binding(name, metadata)
|> maybe_inject_visibility(metadata)
|> maybe_inject_enabled(metadata)
|> maybe_inject_class_member(metadata)
|> maybe_inject_reactive_attrs(metadata)
Expand Down Expand Up @@ -463,30 +463,6 @@ defmodule Lavash.Optimistic.DataAttrTransformer do

defp maybe_inject_state_binding(attrs, _name, _metadata), do: attrs

# ============================================
# Pattern 3: visibility
# ============================================

defp maybe_inject_visibility(attrs, metadata) do
if AttrHelpers.has_attr?(attrs, "data-lavash-visible") do
attrs
else
case AttrHelpers.get_attr_value(attrs, ":if") do
{:expr, "@" <> field_name, _meta} ->
field_atom = String.to_atom(field_name)

if optimistic_boolean?(field_atom, metadata) do
AttrHelpers.add_attr_if_missing(attrs, "data-lavash-visible", {:string, field_name})
else
attrs
end

_ ->
attrs
end
end
end

# ============================================
# Pattern 4: enabled
# ============================================
Expand Down
2 changes: 1 addition & 1 deletion test/integration/dom_directives_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ defmodule Lavash.Integration.DomDirectivesTest do
|> assert_has(css("#list-class-target.static-class.off-class"))
end

test "data-lavash-visible: shows/hides via hidden class", %{session: session} do
test ":if over an optimistic field re-renders via subtree derive", %{session: session} do
# Fixture uses `:if={@hidden_flag}` so when the flag is false the
# element isn't in the DOM at all. After the toggle, it appears.
session = visit(session, "/magic/dom-directives")
Expand Down
38 changes: 11 additions & 27 deletions test/lavash/template/token_transformer_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -409,45 +409,29 @@ defmodule Lavash.Template.TokenTransformerTest do
# Visibility injection
# ============================================

describe "visibility injection" do
test "injects data-lavash-visible for :if={@bool_field} on optimistic boolean" do
describe "visibility injection (retired — subtree derives own :if)" do
test ":if={@bool_field} gets no data-lavash-visible" do
# :if blocks over optimistic fields are re-rendered client-side
# as subtree derives, which handles BOTH directions — a
# class-based directive can't show an element :if removed from
# the DOM. Injecting visible= was dead weight (#127).
tokens = [tag("span", [expr_attr(":if", "@visible")])]
metadata = optimistic_metadata([{:visible, :boolean}])
result = transform(tokens, metadata)

[{:block, :tag, "span", attrs, _children, _, _}] = result
assert Enum.any?(attrs, fn {name, _, _} -> name == "data-lavash-visible" end)
end

test "skips when field is not boolean" do
tokens = [tag("span", [expr_attr(":if", "@count")])]
metadata = optimistic_metadata([{:count, :integer}])
result = transform(tokens, metadata)

[{:block, :tag, "span", attrs, _children, _, _}] = result
refute Enum.any?(attrs, fn {name, _, _} -> name == "data-lavash-visible" end)
end

test "recognizes optimistic calculations as boolean-capable" do
tokens = [tag("span", [expr_attr(":if", "@form_valid")])]
metadata = optimistic_metadata([], calculations: %{form_valid: %{optimistic: true}})
result = transform(tokens, metadata)

[{:block, :tag, "span", attrs, _children, _, _}] = result
assert Enum.any?(attrs, fn {name, _, _} -> name == "data-lavash-visible" end)
end

test "skips when data-lavash-visible already present" do
tokens = [
tag("span", [expr_attr(":if", "@visible"), string_attr("data-lavash-visible", "visible")])
]

test "a hand-written data-lavash-visible passes through untouched" do
tokens = [tag("span", [string_attr("data-lavash-visible", "visible")])]
metadata = optimistic_metadata([{:visible, :boolean}])
result = transform(tokens, metadata)

[{:block, :tag, "span", attrs, _children, _, _}] = result
vis_count = Enum.count(attrs, fn {name, _, _} -> name == "data-lavash-visible" end)
assert vis_count == 1

assert [{"data-lavash-visible", {:string, "visible", _}, _}] =
Enum.filter(attrs, &match?({"data-lavash-visible", _, _}, &1))
end
end

Expand Down
4 changes: 3 additions & 1 deletion test/support/magic/test_directives_fixtures.ex
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ defmodule Lavash.Test.Magic.DomDirectivesLive do
</div>
<button id="toggle-flag" phx-click="toggle_flag">Toggle</button>

<!-- :if={@bool} auto-injects data-lavash-visible -->
<!-- :if={@bool} over an optimistic field rides a subtree
derive: the block re-renders client-side in BOTH
directions (no data-lavash-visible involved, #127) -->
<div id="hidden-section" :if={@hidden_flag}>
Sometimes visible
</div>
Expand Down
Loading