View Source Tablerone (tablerone v1.0.2)

Tablerone is a library for downloading specific tabler icons to the local application, and then rendering them from the file system.

Installation

Tablerone must be configured to know the parent application, in config/config.exs:

import Config

# ...existing config

config :tablerone, :otp_app, <:my_app>

Usage

When using new icons, they can be downloaded using the provided mix task:

mix tablerone.download <icon-name> <icon-name>

To load an icon in a heex component, the following example code may be used:

attr :name, :atom, required: true
attr :class, :any, default: []

def icon(assigns) do
  name = assigns[:name]
  icon_contents = Tablerone.icon(name)

  assigns =
    assign_new(assigns, :icon_contents, fn ->
      class = [class: assigns[:class]] |> Phoenix.HTML.attributes_escape() |> Phoenix.HTML.safe_to_string()
      String.replace(icon_contents, ~r{class="[^"]+"}, class)
    end)

  ~H"""
  <%= Phoenix.HTML.raw(@icon_contents) %>
  """
end

Summary

Functions

Renders the given icon as a Phoenix component. If the icon has not been downloaded to the priv directory of the parent application, icon will raise at run time, with instructions on how to run a mix task to download the icon.

Given an icon name as a string or atom, returns the expected path to the svg file.

Functions

icon(name, type, opts \\ Application.get_all_env(:tablerone))

@spec icon(atom() | binary(), atom(), keyword()) :: binary()

Renders the given icon as a Phoenix component. If the icon has not been downloaded to the priv directory of the parent application, icon will raise at run time, with instructions on how to run a mix task to download the icon.

Examples

iex> icon = Tablerone.icon(:cactus_off, :outline)
iex> match?("<svg" <> _, icon)
true

path(icon_name, type, opts \\ Application.get_all_env(:tablerone))

@spec path(atom() | binary(), atom(), keyword()) :: Path.t()

Given an icon name as a string or atom, returns the expected path to the svg file.

The file is saved to a tablerone directory in the priv dir of the parent application, specified by the :tablerone, :otp_app config.