Bonfire.UI.Common (Bonfire v0.9.12-social-beta.71)
View SourceA library of common utils and helpers used across Bonfire extensions
- Many common functions for web UIs
- Common and generic re-usable components
- Etc
Handy commands
Copyright and License
Copyright (c) 2022 Bonfire Contributors
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License along with this program. If not, see https://www.gnu.org/licenses/.
Summary
Functions
Like update_many_async/3
, but to run several update/preload functions in parallel
Warning: this will set assigns for any/all users who subscribe to them. You want to cast_self/2
instead if dealing with user-specific actions or private data.
Creates a JS command to click all elements matching the specified data-id within a container.
Creates a JS command to click all elements matching the specified data-id within a container.
copies the go
param into a query string, if any
Callback implementation for Bonfire.Common.ExtensionModule.declared_extension/0
.
Generates a deterministic DOM ID based on component type, object ID, context, and parent ID.
Generate a query string adding a go
redirection path to the URI (for redirecting somewhere after auth flows).
It is recommended to use set_go_after/2
where possible instead.
Special LiveView helper function which allows loading LiveComponents in regular Phoenix views: live_render_component(@conn, MyLiveComponent)
Inserts one or many items in an existing stream.
See Phoenix.LiveView.stream_insert/4
for opts.
Generates a random DOM ID.
Renders a HEEx template inline in a controller.
Subscribe to assigns targeted at the current account/user
Save a go
redirection path in the session (for redirecting somewhere after auth flows)
Checks if the socket is connected.
Run a function and expects tuple. If anything else is returned, like an error, a flash message is shown to the user.
Functions
Like update_many_async/3
, but to run several update/preload functions in parallel
Warning: this will set assigns for any/all users who subscribe to them. You want to cast_self/2
instead if dealing with user-specific actions or private data.
Creates a JS command to click all elements matching the specified data-id within a container.
Parameters
container_selector
- CSS selector for the container elementselector
- The type of element or CSS selector to match (defaults to "a")
Example
click_elements_with_data_id("#user_list", "a.follow_button")
Creates a JS command to click all elements matching the specified data-id within a container.
Parameters
container_selector
- CSS selector for the container elementdata_id
- The value of the data-id attribute to match (eg. "follow")element_type
- The type of element to match (defaults to "a")
Example
click_elements_with_data_id("#user_list", "follow", "button")
copies the go
param into a query string, if any
Callback implementation for Bonfire.Common.ExtensionModule.declared_extension/0
.
Generates a deterministic DOM ID based on component type, object ID, context, and parent ID.
Parameters
- component_type: The type or name of the component
- object_id: The ID of the object being displayed
- context: The context where the component is displayed (optional)
- parent_id: The ID of the parent component (optional)
Returns
A string with format: component_type_context_object_id_parent_id
Examples
iex> deterministic_dom_id("modal", "post123", "feed", "container456")
"modal_feed_post123_container456"
iex> deterministic_dom_id("dropdown", "user789")
"dropdown_user789"
Generate a query string adding a go
redirection path to the URI (for redirecting somewhere after auth flows).
It is recommended to use set_go_after/2
where possible instead.
Special LiveView helper function which allows loading LiveComponents in regular Phoenix views: live_render_component(@conn, MyLiveComponent)
TODO: deduplicate if same as render_inline
Inserts one or many items in an existing stream.
See Phoenix.LiveView.stream_insert/4
for opts.
Generates a random DOM ID.
DEPRECATED: Use deterministic_dom_id/4 instead for consistent IDs across renders.
Renders a HEEx template inline in a controller.
Example
use Bonfire.UI.Common.Web, :controller
use Phoenix.Component
def index(conn, _) do
render_inline conn, ~H"<u><%= @current_user.name %></u>"
end
Subscribe to assigns targeted at the current account/user
Save a go
redirection path in the session (for redirecting somewhere after auth flows)
Checks if the socket is connected.
Examples
iex> socket_connected?(%{socket_connected?: true})
true
iex> socket_connected?(%{socket_connected?: false})
false
iex> socket_connected?(%{__context__: %{socket_connected?: true}})
true
iex> socket_connected?(%Phoenix.LiveView.Socket{transport_pid: 1})
true
iex> socket_connected?(%Phoenix.LiveView.Socket{transport_pid: nil})
false
Run a function and expects tuple. If anything else is returned, like an error, a flash message is shown to the user.