{"id":13563241,"url":"https://github.com/cblavier/phx_component_helpers","last_synced_at":"2025-04-04T21:07:47.605Z","repository":{"id":37867580,"uuid":"351216935","full_name":"cblavier/phx_component_helpers","owner":"cblavier","description":"Extensible Phoenix liveview components, without boilerplate","archived":false,"fork":false,"pushed_at":"2024-08-28T11:57:53.000Z","size":114,"stargazers_count":137,"open_issues_count":0,"forks_count":8,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-03-28T20:07:17.454Z","etag":null,"topics":["alpinejs","elixir","liveview","phoenix","tailwindcss"],"latest_commit_sha":null,"homepage":"","language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cblavier.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-03-24T20:39:04.000Z","updated_at":"2025-03-16T22:23:12.000Z","dependencies_parsed_at":"2024-05-01T16:19:55.794Z","dependency_job_id":"f9e98ddc-64c4-4f64-8b99-bce7506866d9","html_url":"https://github.com/cblavier/phx_component_helpers","commit_stats":{"total_commits":125,"total_committers":5,"mean_commits":25.0,"dds":0.08799999999999997,"last_synced_commit":"8eebe62d450e4eb65f00532259e16813b8f3318b"},"previous_names":[],"tags_count":25,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cblavier%2Fphx_component_helpers","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cblavier%2Fphx_component_helpers/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cblavier%2Fphx_component_helpers/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cblavier%2Fphx_component_helpers/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cblavier","download_url":"https://codeload.github.com/cblavier/phx_component_helpers/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247249524,"owners_count":20908212,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["alpinejs","elixir","liveview","phoenix","tailwindcss"],"created_at":"2024-08-01T13:01:16.767Z","updated_at":"2025-04-04T21:07:47.590Z","avatar_url":"https://github.com/cblavier.png","language":"Elixir","funding_links":[],"categories":["Framework Components"],"sub_categories":[],"readme":"# PhxComponentHelpers\n\n[![github](https://github.com/cblavier/phx_component_helpers/actions/workflows/elixir.yml/badge.svg)](https://github.com/cblavier/phx_component_helpers/actions/workflows/elixir.yml)\n[![codecov](https://codecov.io/gh/cblavier/phx_component_helpers/branch/main/graph/badge.svg)](https://codecov.io/gh/cblavier/phx_component_helpers)\n[![Hex pm](http://img.shields.io/hexpm/v/phx_component_helpers.svg?style=flat)](https://hex.pm/packages/phx_component_helpers)\n\n👉 [Demonstration \u0026 Code samples](https://phx-component-helpers.onrender.com)\n\n## Presentation\n\n`PhxComponentHelpers` provides helper functions meant to be used within Phoenix LiveView to make your components more configurable and extensible from templates.\n\nIt provides the following features:\n\n- set HTML, data or phx attributes from component assigns\n- set a bunch of attributes at once with any custom prefix such as `@click` or `x-bind:` (for [alpinejs](https://github.com/alpinejs/alpine) users)\n- validate mandatory attributes\n- set and extend CSS classes from component assigns\n- forward a subset of assigns to child components\n\n## Motivation\n\nWriting a library of stateless components is a great way to improve consistency in both your UI and code and also to get a significant productivity boost.\n\nThe best components can be used _as-is_ without any further configuration, but are versatile enough to be customized from templates or higher level components.\n\nWriting such components is not difficult, but involves a lot of boilerplate code. `PhxComponentHelpers` is here to alleviate the pain.\n\n## Example\n\nA lot of code samples are available [on this site](https://phx-component-helpers.onrender.com), but basically `PhxComponentHelpers` allows you to write components as such:\n\n```elixir\ndefmodule Forms.Button do\n  use Phoenix.Component\n  import PhxComponentHelpers\n\n  def button(assigns) do\n    assigns =\n      assigns\n      |\u003e extend_class(\"bg-blue-700 hover:bg-blue-900 ...\")\n      |\u003e set_attributes([:type, :id, :label], required: [:id])\n      |\u003e set_phx_attributes()\n\n    ~H\"\"\"\n    \u003cbutton {@heex_id} {@heex_type} {@heex_phx_attributes} {@heex_class}\u003e\n      \u003c%= @label %\u003e\n    \u003c/button\u003e\n    \"\"\"\n  end\nend\n```\n\nFrom templates, it looks like this:\n\n```heex\n\u003c.form id=\"form\" phx-submit=\"form_submit\" class=\"divide-none\"\u003e\n\n  \u003c.input_group\u003e\n    \u003c.label for=\"name\" label=\"Name\"/\u003e\n    \u003c.text_input name=\"name\" value={@my.name}/\u003e\n  \u003c/.input_group\u003e\n\n  \u003c.button_group class=\"pt-2\"\u003e\n    \u003c.button type=\"submit\" phx-click=\"btn-click\" label=\"Save\"/\u003e\n  \u003c/.button_group\u003e\n\n\u003c/.form\u003e\n```\n\n## How does it play with the PETAL stack?\n\n[PETAL](https://thinkingelixir.com/petal-stack-in-elixir/) stands for Phoenix - Elixir - TailwindCSS - Alpine.js - LiveView. In recent months it has become quite popular in the Elixir ecosystem and `PhxComponentHelpers` is meant to fit in.\n\n- [TailwindCSS](https://tailwindcss.com) provides a new way to structure CSS, but keeping good HTML hygiene requires to rely on a component-oriented library.\n- [Alpine.js](https://github.com/alpinejs/alpine) is the Javascript counterpart of Tailwind. It lets you define dynamic behaviour right from your templates using HTML attributes.\n\nThe point of developing good components is to provide strong defaults in the component so that they can be used _as_-is, but also to let these defaults be overridden right from the templates.\n\nHere is the definition of a typical Form button, with `Tailwind` \u0026 `Alpine`:\n\n```elixir\ndefmodule Forms.Button do\n  use Phoenix.Component\n  import PhxComponentHelpers\n\n  @css_class \"inline-flex items-center justify-center p-3 w-5 h-5 border \\\n              border-transparent text-2xl leading-4 font-medium rounded-md \\\n              text-white bg-primary hover:bg-primary-hover\"\n\n  def button(assigns) do\n    assigns =\n      assigns\n      |\u003e extend_class(@css_class)\n      |\u003e set_phx_attributes()\n      |\u003e set_prefixed_attributes([\"@click\", \"x-bind:\"],\n        into: :alpine_attributes,\n        required: [\"@click\"]\n      )\n\n    ~H\"\"\"\n    \u003cbutton type=\"button\" {@heex_class} {@heex_alpine_attributes} {@heex_phx_attributes}\u003e\n      \u003c%= render_block(@inner_block) %\u003e\n    \u003c/button\u003e\n    \"\"\"\n  end\nend\n```\n\nThen in your `html.heex` template you can imagine the following code, providing `@click` behaviour and overriding just the few tailwind css classes you need (only `p-*`, `w-*` and `h-*` will be replaced). No `phx` behaviour here, but it's ok, it won't break ;-)\n\n```elixir\n\u003c.button class=\"!p-* p-0 !w-* w-7 !h-* h-7\" \"@click\"=\"$dispatch('closeslideover')\"\u003e\n  \u003c.icon icon={:plus_circle}/\u003e\n\u003c/.button\u003e\n```\n\n## Forms\n\nThis library also provides `Phoenix.HTML.Form` related functions so you can easily write your own `my_form_for` function with your css defaults.\n\n```elixir\ndef my_form_for(options) when is_list(options) do\n  options\n  |\u003e extend_form_class(\"mt-4 space-y-2\")\n  |\u003e Phoenix.LiveView.Helpers.form()\nend\n```\n\nThen you only need to use `PhxComponentHelpers.set_form_attributes/1` within your own form components in order to fetch names \u0026 values from the form. Your template will then look like this:\n\n```heex\n\u003c.my_form_for let={f} for={@changeset} phx-submit=\"form_submit\" class=\"divide-none\"\u003e\n  \u003c.input_group\u003e\n    \u003c.label form={f} field={:name} label=\"Name\"/\u003e\n    \u003c.text_input form={f} field={:name}/\u003e\n  \u003c/.input_group\u003e\n\n  \u003c.button_group class=\"pt-2\"\u003e\n    \u003c.button type=\"submit\" label=\"Save\"/\u003e\n  \u003c/.button_group\u003e\n\u003c/.my_form_for\u003e\n```\n\n## Compared to Surface\n\n[Surface](https://github.com/surface-ui/surface) is a library built on top of Phoenix LiveView. Surface is much more ambitious and complex than `PhxComponentHelpers` (which obviously isn't a framework, just helpers ...).\n\n`Surface` really changes the way you code user interfaces and components (you almost won't be using HTML templates anymore) whereas `PhxComponentHelpers` is just some syntactic sugar to help you use raw `phoenix_live_view`.\n\n## Documentation\n\nAvailable on [https://hexdocs.pm](https://hexdocs.pm/phx_component_helpers)\n\n## Installation\n\nAdd the following to your `mix.exs`.\n\n```elixir\ndef deps do\n  [\n    {:phx_component_helpers, \"~\u003e 1.4.0\"},\n    {:jason, \"~\u003e 1.0\"} # only required if you want to use json encoding options\n  ]\nend\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcblavier%2Fphx_component_helpers","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcblavier%2Fphx_component_helpers","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcblavier%2Fphx_component_helpers/lists"}