Skip to content

Instantly share code, notes, and snippets.

View thiagomajesk's full-sized avatar
💜
Happily doing some Elixir wizardry

Thiago Majesk Goulart thiagomajesk

💜
Happily doing some Elixir wizardry
  • Brazil
View GitHub Profile
@thiagomajesk
thiagomajesk / hooks.js
Created April 11, 2022 03:49
LiveView Tips and Tricks
/*
* Resets a input to a value when a form is updated by LiveView.
* Add phx-hook="FormReset" to the form and phx-reset="" to the input you want to reset.
* The attribute phx-reset must contain the value that the input value will be reseted to.
*/
Hooks.FormReset = {
updated() {
let input = this.el.querySelector('[phx-reset]:not(.invalid-feedback)')
let value = input.getAttribute('phx-reset')
input.value = value
@thiagomajesk
thiagomajesk / purge_html_css.exs
Last active August 8, 2022 20:43
Finds unused CSS classes in HTML files using Elixir
# (!) This is a first-effort/ naive script to find unused CSS in HTML files.
# This should (hopefully) work as the opposite of what you get from PURGE CSS.
# Configure the variables bellow and execute the file with `elixir purge_html_css.exs`.
# Configures the default location to look for the outputed CSS.
# Ideally this file should not be transformed to preserve new lines.
outputed_css = "priv/static/css/app.css"
# Configures the default pattern to look for files that may contain CSS to purge.
# Currently inculdes .eex and .heex files. Change this if necessary.
@thiagomajesk
thiagomajesk / constant.ex
Created April 30, 2024 22:31
Macro-based constants in Elixir
defmodule Constant do
@moduledoc """
Allows generating dynamic functions for the given list of values.
Accepts an enumerable with the of keys and values to be generated as functions.
"""
defmacro __using__(constants) do
[define_constants(constants), define_helpers()]
end
defp define_constants(constants) when is_list(constants) do