GithubHelp home page GithubHelp logo

dwyl / learn-alpine.js Goto Github PK

View Code? Open in Web Editor NEW
12.0 3.0 0.0 300 KB

⛰️ Learn how to use Alpine.js to build declarative + responsive UI in considerably less time!

Home Page: https://alpine-demo.fly.dev

License: GNU General Public License v2.0

HTML 18.07% Elixir 69.97% CSS 2.65% JavaScript 6.13% Dockerfile 2.91% Shell 0.17% Batchfile 0.10%
alpine alpinejs learn tutorial petal phoenix phoenix-framework

learn-alpine.js's People

Contributors

arhell avatar dependabot[bot] avatar nelsonic avatar simonlab avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

mvkvc-os

learn-alpine.js's Issues

How to make `textarea` auto-resize with Alpine.js

At present we have a <textarea> for capturing items in the MVP App:
image

If we input too much text into the <textarea> it starts to scroll which is a poor experience on mobile.

On desktop it kinda works to have a smaller box ...

image

But it would also be nice to tidy up the UX around the input area.

Todo

  • Research how to make the <textarea> resize automatically depending on the content.
  • Should not expand more than the height of the screen minus the keyboard.

Starting point: https://benborgers.com/posts/alpine-textarea-auto-resize + https://youtu.be/z7130uJ3H7w

Accessing x-data from javascript

I was wondering if there was a way to udpate the x-data values on an Alpine.js component from "outside".
An example would be the browser receiving an event from a LiveView server which then apply an action on the Alpine.js component.
This can be useful to have actions from one client displayed to other connected clients in real time.

For example the open value define how to display the I can see you div:

<div x-data="{open: false}" id="dropdown">
    <button @click="open = !open">Toggle</button>
 
    <div x-show="open">I can see you</div>
</div>

In javascript we can then do:

d = document.querySelector('#dropdown');
Alpine.$data(d).open = true; // to show the div
Alpine.$data(d).open = false; // to hide it

see: https://alpinejs.dev/magics/data

Combined with Phoenix LiveView Hooks we could trigger this change on the client from the server.

Adding background color when item is dragged

I have the following Alpine.js attribute define on the table displaying the list of items:

<.table class="mt-3">
  <thead>
    <.tr>
      <.th>Text</.th>
      <.th>Index</.th>
    </.tr>
  </thead>
  <tbody id="items" x-data="">
    <%= for item <- @items do %>
      <.tr
        id={"item-#{item.id}"}
        draggable="true"
        class="draggable"
        x-data="{dragging: false}"
        x-on:dragstart.self="dragging = true; console.log('start dragging')"
        x-on:dragend.self="dragging = false; console.log('stop dragging')"
        x-bind:class="dragging && 'bg-blue-100' : 'bg-yellow-100'"
      >
        <.td class=""><%= item.text %></.td>
        <.td><%= item.index %></.td>
      </.tr>
    <% end %>
  </tbody>
</.table

For each item I'm adding a dragging value which is set to true when the element is dragged (ie dragstart event is triggered).
Depending on this value I'm adding a background color with x-bind:class="dragging && 'bg-blue-100' : 'bg-yellow-100'"

However this does work sometimes but not always! I'm not sure what is the issue yet. Could it be Tailwind not adding the class name as it is computed by Alpine.js? Could it be my Firefox having issue? Or the css/js file not compiling properly?

SPIKE: Drag-and-drop reordering of `items` in a `list`

As outlined in dwyl/app#283 (comment) the next feature on our roadmap is prioritisation of items.
We will achieve this visually by dragging and dropping the item to order them in terms of the priority.

From quick searching, it looks like we can pull off basic drag-and-drop using Alpine.js: 🤞
https://codeblog.trovster.com/2020/05/alpinejs-drag-and-drop/ 📝
https://www.youtube.com/watch?v=zJ4NQaiYxKs 📺
https://codepen.io/trovster/pen/oNjGGMq 👨‍💻

Screen.Recording.2022-09-30.at.10.36.28.mov

Our objective is to SPIKE this ONE feature in a standalone way, see: dwyl/technology-stack#96
Essentially we just want to test that it's _possible to:

a. Have a bunch of items on a page with the following fields:

  • text
  • id
  • index

b. Drag an item to a different position should refresh the ordering i.e. index of the adjacent items
c. If the item from the bottom is moved to the top it will now have index=1

Note: we're not starting our index from 0 as this is not intuitive to non-technical people.

d. All the other items will have their index updated. So we need to get the item.id for each item on the page and update it's new index.
e. Sync this update to another connected client e.g. using LiveView.

Todo

  • Create a new Phoenix App in this repo. call it "app" so that it's easy to copy-paste working code to the MVP later.
  • In that Phoenix App, add the usual setup: Tailwind + Alpine see: mvp/BUILDIT.md#1-create-a-new-phoenix-app
  • Create an items schema but without people or any other unnecessary data/fields. just:
    • id
    • text
    • index
  • Create a LiveView page where we can build this experiement!
  • Update templates to use Tailwind classes (see #4 (comment))
  • Try to implement the most basic drag-and-drop re-ordering and persistence+sync implementation possible.

One More Thing ...

  • Must work flawlessly on Mobile. 📱

Technical Implementation is 100% Flexible at this point!

If you find a more efficient way of implementing this please share!
Right now we are 100% in the research & development phase.

Good luck! 🍀

Deploy on Fly.io?

Now that we have a Phoenix application with the drag and drop feature, it would be nice to test it.

  • Deploy the application on Fly.io (not sure if the flyctl deploy command will work as a Phoenix application and node app coexists 💭 )

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.