Download the PHP package bernskioldmedia/laravel-livewire-widgets without Composer
On this page you can find all versions of the php package bernskioldmedia/laravel-livewire-widgets. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download bernskioldmedia/laravel-livewire-widgets
More information about bernskioldmedia/laravel-livewire-widgets
Files in bernskioldmedia/laravel-livewire-widgets
Package laravel-livewire-widgets
Short Description A framework for adding self-contained Livewire-powered widgets to your application.
License MIT
Homepage https://github.com/bernskioldmedia/laravel-livewire-widgets
Informations about the package laravel-livewire-widgets
A framework for adding self-contained Livewire-powered widgets to your application.
Installation
You can install the package via composer:
After requiring the package, you should import the base styles to your stylesheet. You can do this by importing the
provided widgets.css
file in your app.scss
file:
Tip: Your import should be placed above any Tailwind imports to ensure that the styles are applied correctly. We'd
recommend using the @import
syntax instead of @tailwind
when loading Tailwind's base, components and utilities.
After installing the package you should review the installation instructions for the Laravel Highcharts package if you plan on using the Chart Widget.
Publishing Config and Views
You don't typically need to publish either the config file or the views.
You can publish the config file with:
This is the contents of the published config file:
Optionally, you can publish the views using
Usage
Supported Widget Types
This package supplies two opinionated widget, and one base widget that you can use to build your own.
- Chart Widget. A widget that displays a chart using our Laravel Highcharts package (bundled).
- Statistic Widget. A widget that displays a statistic with a label, a value and a change indicator.
See below for how to use each of these.
Creating a Widget
To create a new widget create a new Livewire class that extends either one of the widget type classes, or the
base Widget
class. A good place to put these is in the App\Livewire\Widgets
namespace.
Note: You do not need to create a view/blade file for your widget if you are using one of the pre-defined widgets ( see the list above).
Lazy Loading
Unfortunately we haven't found a good way to default to lazy loading. As such as we recommend that you
add the #[Lazy]
attribute to your widget classes.
Rendering a widget
To render a widget, you can use the livewire
Blade directive as any other Livewire component:
Chart Widget
Chart widgets are widgets that display a chart. They extend the ChartWidget
class. To use this widget you must
first set up the Laravel Highcharts package. Refer to its
documentation for how to create charts.
To create a chart widget, extend the ChartWidget
class and implement the getChart
method.
Statistic Widget
The Statistic Widget is a widget that displays a statistic with a label, a value and a change indicator.
To create a statistic widget, extend the StatisticWidget
class and implement the getValue
method.
To display a change indicator, implement the ComparesValues
interface and return the previous value from the
getPreviousValue
method.
There are a few additional properties that you can set on the widget:
$showChange
- Default to true. Whether to show the change indicator.$changeAsPercentage
- Default to true. Whether to show the change as a percentage (true) or absolute value (false).$invertedChange
- Default to false. Whether a positive change is good (true) or bad (false).
Widget Sizes
Widgets are rendered in a responsive grid and can be rendered in different widths and heights.
All widths are available in the WidgetWidths
enum, which we recommend over hardcoded string values. These equal:
1/4
- 25% width1/3
- 33% width1/2
- 50% width2/3
- 66% width3/4
- 75% width1/1
- 100% width
Heights are based on the number of rows the widget should take up. As such, the height is dependent on the height of
surrounding widgets. All heights are available in the WidgetHeights
enum, which we recommend over hardcoded strings.
The following heights are available:
1
- 1 row2
- 2 rows3
- 3 rows4
- 4 rows
Note: Rows do not have a base height by themselves.
By default widgets are rendered in a 1/4 width and 1 row height. You can change this by overriding the width
and height
properties on your widget.
You can also set the width and height when loading the widget in your view:
Adding a title
You can add support for a title by implementing the HasTitle
trait on your widget. The Chart Widget and Statistic
Widget both have this included.
This trait adds a title
property to your widget. You can set this property when loading the widget in your view, which
offers a simple way of customizing the title on load. You may also provide a default title by returning a string from
the defaultTitle
method.
The title is available in your view as $widgetTitle
.
Adding a description
You can add support for a description by implementing the HasDescription
trait on your widget. The Chart Widget and
Statistic Widget both have this included. This trait adds a description
property to your widget. You can set this
property when loading the widget in your view, which offers a simple way of customizing the description on load. You may
also provide a default description by returning a string from the defaultDescription
method.
The description is available in your view as $widgetDescription
.
Supporting Filters
You can add support for filters by implementing the Filterable
trait on your widget as well as the SupportsFilters
interface. This trait adds
a filters
property to your widget. You can set this property when loading the widget in your view, which offers a
simple way of customizing the filters on load.
You may also provide default filters by returning an array from the defaultFilters
method.
The default filters will be overridden with any filters you pass when loading the widget.
You may also provide a list of forced filters which cannot be overridden by the user when loading the widget. You do
this by overriding the forcedFilters
method.
The trait also adds convenience methods for interacting with filters:
Defining a custom view
Because of the helper methods we include, adding a view is slightly different. Instead of using the render
method, you
define the static view
method and return the view name.
You can pass additional data to the view by returning an array from the getViewData
method:
Placeholders
The package ships with default placeholders for each widget type, as well as a plain base widget placeholder. To use
your our placeholder view, return the view name from the placeholderView
method:
You can pass data to the placeholder view by returning an array from the placeholderData
method. You should merge your
data with the parent method's data.
Customizing the Styles
We have tried to make the styles we ship as unopinionated as possible to only provide the structure you most likely will need regardless of appearance. As such, you will want to apply additional customization to the widget classes to make them fit your application.
You may customize the widget grid gap and widget padding by using the following CSS variables:
Responsive Widget Grid
By default, widgets are rendered in a responsive grid. The grid is based on CSS Grid and is responsive in a slightly opinionated way. This means that each widget only has a specific width configured, which is the width it will take up on desktop. On smaller screens, the width is automatically adjusted.
The base grid is:
- 2 columns by default
- 4 columns on
md
screens (and above) - 12 columns on
lg
screens (and above)
Each Widget Width corresponds to a number of columns:
1/4
- 1 column1/3
- 1 column (base), 2 columns (md
), 4 columns (lg
)1/2
- 1 column (base), 2 columns (md
), 6 columns (lg
)2/3
- 2 columns (base), 8 columns (lg
)3/4
- 2 columns (base), 3 columns (md
), 9 columns (lg
)1/1
- 2 columns (base), 4 columns (md
), 12 columns (lg
)
You may customize this setup by overriding the corresponding width classes in your own stylesheet.
Testing
Changelog
Please see CHANGELOG for more information on what has changed recently.
Credits
- Bernskiold Media
- Erik Bernskiold
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-livewire-widgets with dependencies
bernskioldmedia/laravel-highcharts Version ^0.1.0
illuminate/contracts Version ^10.0
livewire/livewire Version ^3.0