dwyl / learn-alpine.js Goto Github PK
View Code? Open in Web Editor NEW⛰️ 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
⛰️ 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
At present we have a <textarea>
for capturing items
in the MVP App:
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 ...
But it would also be nice to tidy up the UX around the input area.
<textarea>
resize automatically depending on the content.keyboard
.Starting point: https://benborgers.com/posts/alpine-textarea-auto-resize + https://youtu.be/z7130uJ3H7w
A select box with auto-completion / search is a UI component we are going to need very soon.
Building a custom select input with Alpine.js and Tailwind CSS: https://youtu.be/L_f9gNaSCRE
https://github.com/danharrin/alpine-tailwind-components/blob/main/select/index.html
incorrect path
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.
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?
I'm trying to use Alpine.js events in a LiveView template. However something is not set correctly as the events are not working properly.
Reading http://blog.pthompson.org/alpine-js-and-liveview and I need to rethink how the page is structured to see if I need to use Phoenix Hooks
As noted in: #5 (review) the test coverage in this repo is quite low.
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
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
from0
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
.
Phoenix
App in this repo. call it "app" so that it's easy to copy-paste working code to the MVP
later.Phoenix
App, add the usual setup: Tailwind + Alpine see: mvp/BUILDIT.md#1-create-a-new-phoenix-appitems
schema but without people
or any other unnecessary data/fields. just:
id
text
index
LiveView
page where we can build this experiement!If you find a more efficient way of implementing this please share!
Right now we are 100% in the research & development phase.
Good luck!
While searching a solution for #9 I found this post: https://elixirforum.com/t/petl-stack-is-alpinejs-still-relevant/43306
I haven't read all of the comments but I'm keen to see if I can use Hooks and JS command with LiveView to create the drag and drop
feature and to compare it with the Tailwind version.
Alpine.js
+ Tailwind
Related to: dwyl/phoenix-liveview-stopwatch#1
Now that we have a Phoenix application with the drag and drop feature, it would be nice to test it.
flyctl deploy
command will work as a Phoenix application and node app coexists A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.