liveview-native / live_view_native_stylesheet

MIT License
10 stars 4 forks source link

EEx interpolation #42

Closed bcardarella closed 7 months ago

bcardarella commented 7 months ago

Introduces EEx interpolation on the rules document prior to passing to the format specific parser. This means there is a two-phase process.

The tests were also simplified. As ~RULES is now simply only responsible for interpolating with EEx then passing the resulting document to the format specific rules parser. We didn't need the rules tests in this lilb.

This means we can drop the idea of "helpers" as we can execute Elixir code in the EEx interpolation:

"frame:" <> dims do
  frame(height: <%= String.split(dims, ":") |> List.first() %>, width: <%= String.split(dims, ":") |> List.last() %>)
end

and because it's just Elixir we can extract these into function helpers in the sheet itself:

defmodule MySheet do
  use LiveViewNative.Stylesheet, :swiftui

  ~SHEET"""
  "frame:" <> dims do
    frame(height: <%= extract_dim(dims, 0) %>, width: <%= extract_dim(dims, 1) %>)
  end
  """

  defp extract_dims(dims, index) do
    dims
    |> String.split("-")
    |> Enum.at(index)
  end
end
bcardarella commented 7 months ago

@NduatiK I'm thinking we can drop the helpers from: https://github.com/liveview-native/live_view_native_stylesheet/blob/main/lib/live_view_native/stylesheet/rules_parser/helpers.ex#L9-L16

as well as the additional_helpers option, but want to get your thoughts.

On the rules parser side we'll still need attr and event. I'd also like to look at using @foo-bar for the attr shorthand. I'd like to find one for events too