tailwindcss-stimulus-components

repository·main·Indexed 23 days ago

https://github.com/excid3/tailwindcss-stimulus-components

A collection of TailwindCSS-ready UI components powered by StimulusJS controllers, including Tabs, Modals, Dropdowns, Alerts, Autosave, Color Preview, Popovers, Toggles, and Slideovers. Version 6.1.4 provides unstyled, utility-first components that rely on data attributes for functionality, offering a flexible alternative to Bootstrap for TailwindCSS users.

Tokens
10.2K
Snippets
25
Records
40
Agent score
80%

What's inside tailwindcss-stimulus-components

  1. Styling and Data Attributes

    main

    The components are unstyled by default; they do not require specific CSS classes to function. Instead, they rely on correctly defined data- attributes to trigger actions and locate targets.

    Note that some components, such as the Modal, require specific structural styles (like separate container and background elements) to ensure proper browser behavior and layout control.

  2. Handle autosave submissions server-side

    main
    When the form is autosaved, the server receives a request. To distinguish between a background autosave (draft) and a manual user submission (publish), check the params[:commit] text. This text corresponds to the value of the submit button that was clicked. For managing drafts effectively on the backend, the use of a gem like Draftsman is recommended.
  3. Use the Autosave component in a form

    main

    Apply the autosave controller to a form and define a target for the form itself. To trigger saves, add an action (like keyup->autosave#save) to input fields. The component uses form.requestSubmit() to ensure Turbo events are fired. You should listen for turbo:submit-end and turbo:fetch-request-error to handle success and error states respectively.

    <%= form_with(model: post, data: { controller: "autosave", autosave_target: "form", action: "turbo:submit-end->autosave#success turbo:fetch-request-error->autosave#error" }) do |form| %>
      <div class="form-group">
        <%= form.label :title %>
        <%= form.text_field :title, class: 'form-control', data: { action: "keyup->autosave#save" } %>
      </div>
    
      <div data-autosave-target="status"></div>
    
      <%= form.submit %>
    <% end %>
  4. Register the Autosave Stimulus component

    main

    To use the Autosave component, you must first register it with your Stimulus application. This component provides automatic debounced form submission for autosave functionality, with a default debounce delay of 1000ms.

    import { Autosave } from "tailwindcss-stimulus-components"
    application.register('autosave', Autosave)
  5. Register and use the Modal component

    main

    To use the Modal component, register it with your Stimulus application and use the modal controller on a wrapper element. The component requires a <dialog> element marked with the data-modal-target="dialog" attribute. You can control the modal using the modal#open and modal#close actions.

    import { Modal } from "tailwindcss-stimulus-components"
    application.register('modal', Modal)
    <div data-controller="modal">
      <dialog data-modal-target="dialog" class="p-8 rounded-lg backdrop:bg-black/80 m-auto">
        <p>This modal dialog has a groovy backdrop!</p>
        <button autofocus data-action="modal#close" class="px-2.5 py-1 bg-blue-500 text-white text-sm rounded">Close</button>
      </dialog>
    
      <button data-action="modal#open" class="bg-blue-500 hover:bg-blue-700 text-white text-sm font-bold py-1 px-2.5 rounded">Open modal</button>
    </div>
  6. Use the Color Picker and Preview component

    main

    The color-preview Stimulus controller provides a color picker interface that updates a preview element's color or background color.

    By default, the controller updates the backgroundColor of the target element. It also automatically calculates the contrast using a YIQ algorithm to ensure foreground text remains readable by switching between black and white text colors.

    To update the text color instead of the backgroundColor, add the data-color-preview-style-value="color" attribute to the controller element.

    <div class="mt-3 flex items-center" data-controller="color-preview">
      <!-- The preview element -->
      <p data-color-preview-target="preview" class="h-10 w-10 mr-2 rounded-full text-2xl text-white text-center" style="background-color: #ba1e03; color: #fff; padding-top: 1px;">A</p>
      
      <span class="ml-2">
        <div class="flex rounded-md shadow-sm">
          <span class="inline-flex items-center px-3 rounded-l-md border border-r-0 border-gray-300 bg-gray-50 text-gray-500">#</span>
          <!-- The color input -->
          <input data-action="input->color-preview#update" 
            data-color-preview-target="color" 
            id="hex_color_bg" 
            name="hex_color_bg" 
            type="color" 
            value="#ba1e03" 
            class="focus:ring-indigo-500 focus:border-indigo-500 block shadow-sm sm:text-sm border-gray-300 flex-1 rounded-r-md mt-0 w-24 h-8 px-1 py-1 border" />
        </div>
      </span>
    </div>
  7. Use the Popover component

    main

    The Popover component is implemented by wrapping a trigger element and a content element within a container controlled by data-controller="popover".

    Implementation Details

    • Triggering: Use data-action="mouseenter->popover#show mouseleave->popover#hide" on the controller container to show/hide the popover on hover.
    • Content Target: You must define the popover content element using data-popover-target="content".
    • Transitions: You can control the entry and exit animations using data-transition-enter, data-transition-enter-from, data-transition-enter-to, data-transition-leave, data-transition-leave-from, and data-transition-leave-to on the content element.
    • Auto-dismiss: Use the data-popover-dismiss-after-value attribute to specify a duration in milliseconds after which the popover will automatically dismiss. If not provided, it defaults to undefined (no auto-dismiss).
    <div class="inline-block relative cursor-pointer" data-controller="popover" data-action="mouseenter->popover#show mouseleave->popover#hide">
      <span class="underline">Hover me</span>
      <div class="hidden absolute left-0 bottom-7 w-max bg-white border border-gray-200 shadow rounded p-2"
           data-popover-target="content"
           data-transition-enter="transition-opacity ease-in-out duration-300"
           data-transition-enter-from="opacity-0"
           data-transition-enter-to="opacity-100"
           data-transition-leave="transition-opacity ease-in-out duration-300"
           data-transition-leave-from="opacity-100"
           data-transition-leave-to="opacity-0"
        >
        This popover shows on hover
      </div>
    </div>