heureka-agency/switcheroo

TALL-stack currency switcher for LiveWire.

1.2 2024-08-12 22:24 UTC

This package is auto-updated.

Last update: 2024-09-12 22:38:03 UTC


README

Latest Version on Packagist Total Downloads

Under the hood, Switcheroo is a single Livewire component with some AlpineJS magic sprinkled on top. The component uses and requires TailwindCss for it's styling, but you're free to change to any other CSS framework or even native CSS if you wish.

This package does not offer any currency conversion features yet.

Installation

You can install the package via composer:

composer require heureka-agency/switcheroo

You will also need to install the flag-icons npm package to display currency flags:

npm install flag-icons

Usage

Config

First, publish the Switcheroo config with the following command:

php artisan vendor:publish --tag=switcheroo-config
return [
    /**
     * Add any currencies you would like to handle with Switcheroo.
     * You can select all currencies using \HeurekaAgency\Switcheroo\Enums\Currency::all()
     */
    'enabled_currencies' => [
        \HeurekaAgency\Switcheroo\Enums\Currency::EUR,
        \HeurekaAgency\Switcheroo\Enums\Currency::USD,
        \HeurekaAgency\Switcheroo\Enums\Currency::GBP,
    ],

    /**
     * Set the default currency of the currency switcher.
     */
    'default_currency' => \HeurekaAgency\Switcheroo\Enums\Currency::EUR
];

Including in your app

Make sure to import the Switcheroo CSS in your app.css:

@import "../../vendor/heureka-agency/switcheroo/resources/css/switcheroo.css";

Then simply include the Switcheroo component wherever you'd like:

<livewire:switcheroo::currency-switcher /> 

Events

Switcheroo sends a single Livewire event every time a currency is selected. You can freely access this event from any other Livewire component or even from Javascript.

#[On('currency-selected')]
public function yourCurrencySwitchHandler(string $currency): void
{
    $currency = Currency::from($currency);
    // any other business logic
} 

The selected currency is also stored in local storage using Alpine.$persist.

Views

You're free to publish the Switcheroo views to customize the look and feel of the currency switcher:

php artisan vendor:publish --tag=switcheroo-views 

Translations

Switcheroo is multilingual ready! Not that it has any readable strings to translate, but nonetheless, you can publish the language files using:

php artisan vendor:publish --tag=switcheroo-translations 

Future plans

  • create a helper to automatically convert currencies based on the current selection
  • add support for language and country selection
  • add support for native language display of currencies/languages/countries

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email thar.tristan@heureka-kreativ.hu instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.