Packages

Layout and style that's easy to refactor, all without thinking about CSS.

Current section

Files

Jump to
legos src legos@background.erl
Raw

src/legos@background.erl

-module(legos@background).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/legos/background.gleam").
-export([color/1, image/1, uncropped/1, tiled/1, tiled_x/1, tiled_y/1, step/1, percent/2, px/2, gradient/2]).
-export_type([direction/0, step/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-type direction() :: to_up |
to_down |
to_right |
to_top_right |
to_bottom_right |
to_left |
to_top_left |
to_bottom_left |
{to_rad, float()} |
{to_degrees, integer()}.
-type step() :: {color_step, legos@internal@model:color()} |
{percent_step, integer(), legos@internal@model:color()} |
{px_step, integer(), legos@internal@model:color()}.
-file("src/legos/background.gleam", 11).
?DOC(" A background color attribute\n").
-spec color(legos@internal@model:color()) -> legos@internal@model:attribute(any(), any()).
color(Clr) ->
{style_class,
legos@internal@flag:bg_color(),
{colored,
<<"bg-"/utf8,
(legos@internal@model:format_color_class(Clr))/binary>>,
<<"background-color"/utf8>>,
Clr}}.
-file("src/legos/background.gleam", 23).
?DOC(" Resize the image to fit the containing element while maintaining proportions and cropping the overflow.\n").
-spec image(binary()) -> legos@internal@model:attribute(legos@internal@model:aligned(), any()).
image(Src) ->
{attr,
lustre@attribute:style(
<<"background"/utf8>>,
<<<<"url(\""/utf8, Src/binary>>/binary,
"\") center / cover no-repeat"/utf8>>
)}.
-file("src/legos/background.gleam", 31).
?DOC(" A centered background image that keeps its natural proportions, but scales to fit the space.\n").
-spec uncropped(binary()) -> legos@internal@model:attribute(legos@internal@model:aligned(), any()).
uncropped(Src) ->
{attr,
lustre@attribute:style(
<<"background"/utf8>>,
<<<<"url(\""/utf8, Src/binary>>/binary,
"\") center / contain no-repeat"/utf8>>
)}.
-file("src/legos/background.gleam", 39).
?DOC(" Tile an image in the x and y axes.\n").
-spec tiled(binary()) -> legos@internal@model:attribute(legos@internal@model:aligned(), any()).
tiled(Src) ->
{attr,
lustre@attribute:style(
<<"background"/utf8>>,
<<<<"url(\""/utf8, Src/binary>>/binary, "\") repeat"/utf8>>
)}.
-file("src/legos/background.gleam", 44).
?DOC(" Tile an image in the x axis.\n").
-spec tiled_x(binary()) -> legos@internal@model:attribute(legos@internal@model:aligned(), any()).
tiled_x(Src) ->
{attr,
lustre@attribute:style(
<<"background"/utf8>>,
<<<<"url(\""/utf8, Src/binary>>/binary, "\") repeat-x"/utf8>>
)}.
-file("src/legos/background.gleam", 49).
?DOC(" Tile an image in the y axis.\n").
-spec tiled_y(binary()) -> legos@internal@model:attribute(legos@internal@model:aligned(), any()).
tiled_y(Src) ->
{attr,
lustre@attribute:style(
<<"background"/utf8>>,
<<<<"url(\""/utf8, Src/binary>>/binary, "\") repeat-y"/utf8>>
)}.
-file("src/legos/background.gleam", 73).
?DOC(" Create a color step for gradients\n").
-spec step(legos@internal@model:color()) -> step().
step(Color) ->
{color_step, Color}.
-file("src/legos/background.gleam", 78).
?DOC(" Create a percentage-based color step for gradients\n").
-spec percent(integer(), legos@internal@model:color()) -> step().
percent(Pct, Color) ->
{percent_step, Pct, Color}.
-file("src/legos/background.gleam", 83).
?DOC(" Create a pixel-based color step for gradients\n").
-spec px(integer(), legos@internal@model:color()) -> step().
px(Pixels, Color) ->
{px_step, Pixels, Color}.
-file("src/legos/background.gleam", 185).
-spec flip_angle(integer()) -> integer().
flip_angle(Angle) ->
case (Angle + 180) > 360 of
true ->
(Angle + 180) - 360;
false ->
Angle + 180
end.
-file("src/legos/background.gleam", 164).
-spec direction_class_prop(direction()) -> {binary(), binary()}.
direction_class_prop(Direction) ->
case Direction of
to_up ->
{<<"to-up"/utf8>>, <<"to up"/utf8>>};
to_down ->
{<<"to-down"/utf8>>, <<"to down"/utf8>>};
to_right ->
{<<"to-right"/utf8>>, <<"to right"/utf8>>};
to_top_right ->
{<<"to-top-right"/utf8>>, <<"to top right"/utf8>>};
to_bottom_right ->
{<<"to-bottom-right"/utf8>>, <<"to bottom right"/utf8>>};
to_left ->
{<<"to-left"/utf8>>, <<"to left"/utf8>>};
to_top_left ->
{<<"to-top-left"/utf8>>, <<"to topleft"/utf8>>};
to_bottom_left ->
{<<"to-bottom-left"/utf8>>, <<"to bottom left"/utf8>>};
{to_rad, Angle} ->
{legos@internal@model:float_class(Angle),
<<(gleam_stdlib:float_to_string(Angle))/binary, "rad"/utf8>>};
{to_degrees, Angle@1} ->
{legos@internal@model:float_class(erlang:float(flip_angle(Angle@1))),
<<(erlang:integer_to_binary(flip_angle(Angle@1)))/binary,
"deg"/utf8>>}
end.
-file("src/legos/background.gleam", 91).
?DOC(
" A linear gradient.\n"
" First you need to specify what direction the gradient is going by providing an angle in radians.\n"
" `0.0` is up and `pi` is down.\n"
" The colors will be evenly spaced.\n"
).
-spec gradient(direction(), list(step())) -> legos@internal@model:attribute(any(), any()).
gradient(Direction, Steps) ->
To_bg_color = fun(Clr) ->
{style_class,
legos@internal@flag:bg_color(),
{colored,
<<"bg-"/utf8,
(legos@internal@model:format_color_class(Clr))/binary>>,
<<"background-color"/utf8>>,
Clr}}
end,
case Steps of
[] ->
no_attribute;
[{color_step, Clr@1}] ->
To_bg_color(Clr@1);
[{percent_step, _, Clr@2}] ->
To_bg_color(Clr@2);
[{px_step, _, Clr@3}] ->
To_bg_color(Clr@3);
_ ->
{Direction_class, Direction_prop} = direction_class_prop(Direction),
Class_parts = begin
_pipe = [Direction_class],
_pipe@1 = lists:append(
_pipe,
gleam@list:map(Steps, fun(Step) -> case Step of
{color_step, Clr@4} ->
legos@internal@model:format_color_class(
Clr@4
);
{percent_step, Position, Clr@5} ->
<<<<<<(legos@internal@model:format_color_class(
Clr@5
))/binary,
"-"/utf8>>/binary,
(erlang:integer_to_binary(Position))/binary>>/binary,
"-pct"/utf8>>;
{px_step, Position@1, Clr@6} ->
<<<<<<(legos@internal@model:format_color_class(
Clr@6
))/binary,
"-"/utf8>>/binary,
(erlang:integer_to_binary(
Position@1
))/binary>>/binary,
"-px"/utf8>>
end end)
),
gleam@string:join(_pipe@1, <<"-"/utf8>>)
end,
Color_parts = begin
_pipe@2 = gleam@list:map(Steps, fun(Step@1) -> case Step@1 of
{color_step, Clr@7} ->
legos@internal@model:format_color(Clr@7);
{percent_step, Position@2, Clr@8} ->
<<<<<<(legos@internal@model:format_color(Clr@8))/binary,
" "/utf8>>/binary,
(erlang:integer_to_binary(Position@2))/binary>>/binary,
"%"/utf8>>;
{px_step, Position@3, Clr@9} ->
<<<<<<(legos@internal@model:format_color(Clr@9))/binary,
" "/utf8>>/binary,
(erlang:integer_to_binary(Position@3))/binary>>/binary,
"px"/utf8>>
end end),
_pipe@3 = gleam@list:prepend(_pipe@2, Direction_prop),
gleam@string:join(_pipe@3, <<", "/utf8>>)
end,
{style_class,
legos@internal@flag:bg_gradient(),
{single,
<<"bg-grad-"/utf8, Class_parts/binary>>,
<<"background-image"/utf8>>,
<<<<"linear-gradient("/utf8, Color_parts/binary>>/binary,
")"/utf8>>}}
end.