GithubHelp home page GithubHelp logo

marcoroth / turbo_power Goto Github PK

View Code? Open in Web Editor NEW
346.0 6.0 15.0 1.88 MB

Power-pack for Turbo Streams

License: MIT License

JavaScript 6.32% TypeScript 93.68%
cableready hotwire turbo turbostreams rails streams hacktoberfest

turbo_power's Introduction

TurboPower

NPM Version NPM Downloads NPM Bundle Size

Getting Started

turbo_power is a power-pack for Turbo Streams. It provides Turbo Streams with a bunch of new actions and additionally adds the morph action from turbo-morph.

Note: Requires Turbo 7.2+

Getting Started

yarn add turbo_power
// application.js
import * as Turbo from '@hotwired/turbo'

+import TurboPower from 'turbo_power'
+TurboPower.initialize(Turbo.StreamActions)

Getting Started with Rails

Checkout the instructions in the turbo_power-rails repo.

Getting Started with Django

Checkout the doc of the django-turbo-helper repo.

Custom Actions

DOM Actions

  • turbo_stream.graft(target, parent, **attributes)
  • turbo_stream.morph(target, html = nil, **attributes, &block)
  • turbo_stream.inner_html(target, html = nil, **attributes, &block)
  • turbo_stream.insert_adjacent_html(target, html = nil, position: 'beforeend', **attributes, &block)
  • turbo_stream.insert_adjacent_text(target, text, position: 'beforebegin', **attributes)
  • turbo_stream.outer_html(target, html = nil, **attributes, &block)
  • turbo_stream.text_content(target, text, **attributes)
  • turbo_stream.set_meta(name, content)

Attribute Actions

  • turbo_stream.add_css_class(target, classes, **attributes)
  • turbo_stream.remove_attribute(target, attribute, **attributes)
  • turbo_stream.remove_css_class(target, classes, **attributes)
  • turbo_stream.set_attribute(target, attribute, value, **attributes)
  • turbo_stream.set_dataset_attribute(target, attribute, value, **attributes)
  • turbo_stream.set_property(target, property, value, **attributes)
  • turbo_stream.set_style(target, name, value, **attributes)
  • turbo_stream.set_styles(target, styles, **attributes)
  • turbo_stream.set_value(target, value, **attributes)
  • turbo_stream.toggle_css_class(target, classes, **attributes)
  • turbo_stream.replace_css_class(target, from, to, **attributes)

Event Actions

  • turbo_stream.dispatch_event(target, name, detail: {}, **attributes)

Form Actions

  • turbo_stream.reset_form(target, **attributes)

Storage Actions

  • turbo_stream.clear_storage(type, **attributes)
  • turbo_stream.clear_local_storage(**attributes)
  • turbo_stream.clear_session_storage(**attributes)
  • turbo_stream.remove_storage_item(key, type, **attributes)
  • turbo_stream.remove_local_storage_item(key, **attributes)
  • turbo_stream.remove_session_storage_item(key, **attributes)
  • turbo_stream.set_storage_item(key, value, type, **attributes)
  • turbo_stream.set_local_storage_item(key, value, **attributes)
  • turbo_stream.set_session_storage_item(key, value, **attributes)

Browser Actions

  • turbo_stream.reload(**attributes)
  • turbo_stream.scroll_into_view(**attributes)
    • turbo_stream.scroll_into_view(targets)
    • turbo_stream.scroll_into_view(targets, align_to_top)
    • turbo_stream.scroll_into_view(targets, behavior:, block:, inline:)
  • turbo_stream.set_focus(target, **attributes)
  • turbo_stream.set_title(title, **attributes)

Document Actions

  • turbo_stream.set_cookie(cookie, **attributes)
  • turbo_stream.set_cookie_item(key, value, **attributes)

Browser History Actions

  • turbo_stream.history_back(**attributes)
  • turbo_stream.history_forward(**attributes)
  • turbo_stream.history_go(delta, **attributes)
  • turbo_stream.push_state(url, title = nil, state = nil, **attributes)
  • turbo_stream.replace_state(url, title = nil, state = nil, **attributes)

Debug Actions

  • turbo_stream.console_log(message, level = :log)
  • turbo_stream.console_table(data, columns)

Notification Actions

  • turbo_stream.notification(title, **options)

Turbo Actions

  • turbo_stream.redirect_to(url, turbo_action = nil, turbo_frame = nil, **attributes)
  • turbo_stream.turbo_clear_cache()

Turbo Progress Bar Actions

  • turbo_stream.turbo_progress_bar_show()
  • turbo_stream.turbo_progress_bar_hide()
  • turbo_stream.turbo_progress_bar_set_value(value)

Turbo Frame Actions

  • turbo_stream.turbo_frame_reload(frame_id)
  • turbo_stream.turbo_frame_set_src(frame_id, src)

Previous Art

TurboPower is heavily inspired by CableReady and its operations. This library aims to bring the same level of operation-diversity to Turbo Streams.

Development

To run the test runner:

yarn build
yarn test

Acknowledgments

turbo_power is MIT-licensed open-source software from Marco Roth.

turbo-morph is MIT-licensed open-source software from Marco Roth.

Turbo is MIT-licensed open-source software from Basecamp.

turbo_power's People

Contributors

adrianthedev avatar airblade avatar dependabot[bot] avatar drnic avatar leonvogt avatar lgtm-com[bot] avatar lxxxvi avatar marcoroth avatar michael-yin avatar minimul avatar valentinorusconi avatar woto avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

turbo_power's Issues

Implement `turbo_set_progress_bar` action

Add an action/multiple actions for controlling the Turbo progress bar

Turbo.navigator.adapter.progressBar.setValue(value)
Turbo.navigator.adapter.progressBar.show()
Turbo.navigator.adapter.progressBar.hide()

Implement Action to set a `window.onbeforeunload` callback

Not sure how useful this is, but it might be interesting to have an action to set a window.onbeforeunload callback. This can be handy to set from the server-side when you are sending down some data which is not persisted yet. And with this you would get a warning when you attempt to close the window/tab. Like:

Screenshot 2022-11-29 at 02 49 30

The example above can be achieved with this snippet:

window.onbeforeunload = function() {
  return true;
}

Obviously we would also need an action to clear the event-listener again.

Add Django Content

@marcoroth

I am a Django developer and maintainer of django-turbo-helper, which is turbo helper for Django.

Inspired by "turbo_power-rails", I just added some custom actions to the project, so Django developer can also use morph, turbo_frame_reload and other actions in Django, with the same way "turbo_power-rails" does.

Here is the source code https://github.com/rails-inspire-django/django-turbo-helper/blob/main/src/turbo_helper/turbo_power.py if you are interested.

I want to add the link to the doc of this project, to tell people this project can also work in Django, and I am sure many Django developers will like it. ๐Ÿ‘

If you think this is good to go, please let me know and I will create PR in a bit.

Thanks for your great work on this project.

Feature request: Remove morph as a default action.

I'd speculate (I admit that this surmise is based on no actually data but just a professional hunch) that the morph custom action is rarely used but in turn carries a dependency, the turbo-morph NPM package.

Should it not then be required but opt-in?

Thanks @marcoroth for your consideration.

Rails importmap and v0.3.0

  1. I endeavored to move up to v0.3.0 just by manually adjusting the Rails config/importmap.rb turbo_power version, which after its setting yielded a browser console error of:

Uncaught TypeError: Failed to resolve module specifier "turbo-morph". Relative references must start with either "/", "./", or "../".

There was an error with either of the below CDNs.

# config/importmap.rb 
pin 'turbo_power', to: 'https://ga.jspm.io/npm:[email protected]/dist/turbo_power.js'
# pin 'turbo_power', to: 'https://cdn.jsdelivr.net/npm/[email protected]/dist/turbo_power.js'
  1. I figured it was a missing turbo-morph dependency so I just re-enabled the jspm.io CDN in config/importmap.rb and made an attempt to use the Rails' bin/importmap script to handle fetching the correct turbo-morph dependency and that venture yielded these errors:
bin/importmap unpin turbo_power
Couldn't find any packages in ["turbo_power"] on jspm
bin/importmap pin turbo_power
Couldn't find any packages in ["turbo_power"] on jspm

Thanks @marcoroth.

Implement `alignToTop` and `scrollIntoViewOptions` for `scroll_into_view` action

Currently the scroll_into_view action takes no argument and just calls element.scrollIntoView() on all targets:

export function scroll_into_view(this: StreamElement) {
this.targetElements.forEach((element: Element) => element.scrollIntoView())
}

Looking at the reference it shows that there are also other ways to call scrollIntoView(), like:

scrollIntoView()
scrollIntoView(alignToTop)
scrollIntoView(scrollIntoViewOptions)

It would make sense to also implement the alignToTop and scrollIntoViewOptions arguments.

Reference:

Action `redirect_to` should use Turbo by default

Currently you need to pass turbo="true" to the <turbo-stream> element in order that is uses Turbo.visit():

<turbo-stream turbo="true" url="http://localhost:3000/" action="redirect_to"><template></template></turbo-stream>

It should use Turbo.visit() by default unless turbo="false" gets explicitly passed to the stream element.

The action already accounts for when Turbo/Turbolinks is not installed in the application and will fallback to window.location.href = url

export function redirect_to(this: StreamElement) {

`redirect_to` Action: rename `action` attribute to `action_name`

The redirect_to action has an action attribute which is being passed to Turbo.visit(..., { action }) to control the Turbo visit behaviour.

But the action attribute name is clashing with the <turbo-stream> element, since it's using the action attribute for determining the stream action to execute.

We should rename the attribute for the redirect_to action to either action_name or turbo_action.

Remove `Turbolinks` from all actions

The Turbolinks cases in all actions were taken over from the CableReady operations.

But technically Turbolinks isn't really compatible with TurboPower since you actually need to have Turbo installed in your app if you want to use Custom Turbo Stream Actions.

[Feature Request] Add or improve history actions

Thanks for the great lib!

It would be nice to have a shortcut to modify Turbo.navigator.history. I find it an essential action since the Back button is not doing what is expected when the browser history is modified without touching Turbo.navigator.history. As a workaround, the invoke action can be used at the moment.

More on the issue: https://discuss.hotwired.dev/t/back-button-not-working/3012

P.S. I'd really love to help but I'm not very familiar with Typescript yet :/

Where is v0.6.0 in rubygems?

There is latest v0.6.0 release and I try to request it in Gemfile:

gem "turbo_power", "~> 0.6.0"

But its not on rubygems https://rubygems.org/gems/turbo_power ? Did it get yanked or not uploaded?

$ bundle update
Could not find gem 'turbo_power (~> 0.6.0)' in rubygems repository https://rubygems.org/ or installed locally.

The source contains the following gems matching 'turbo_power':
  * turbo_power-0.1.0
  * turbo_power-0.1.1
  * turbo_power-0.1.2
  * turbo_power-0.1.3
  * turbo_power-0.1.5
  * turbo_power-0.1.6
  * turbo_power-0.2.0
  * turbo_power-0.3.0
  * turbo_power-0.3.1
  * turbo_power-0.4.0
  * turbo_power-0.5.0

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.