diff --git a/examples/views/js_commands/js_commands.css b/examples/views/js_commands/js_commands.css new file mode 100644 index 0000000..df22dad --- /dev/null +++ b/examples/views/js_commands/js_commands.css @@ -0,0 +1,45 @@ +.copied::after { + content: "✓"; + margin-left: 8px; +} + +#copy-text { + font-size: 0.75rem; + background-color: #f0f0f0; + padding: 1em; +} + +#counter { + padding: 1em; + margin-block: 1em; + background-color: #eee; + border-radius: 1em; + width: 16ch; +} + +#focus-form { + margin-block: 1em; + display: flex; + flex-direction: column; + gap: 0.25em; + width: 50ch; +} + + +@keyframes bounce { + + 0%, + 100% { + transform: translateY(-25%); + animation-timing-function: cubic-bezier(0.8, 0, 1, 1); + } + + 50% { + transform: translateY(0); + animation-timing-function: cubic-bezier(0, 0, 0.2, 1); + } +} + +.bounce { + animation: bounce 1s infinite; +} \ No newline at end of file diff --git a/examples/views/js_commands/js_commands.html b/examples/views/js_commands/js_commands.html index c5fa3ec..6b842da 100644 --- a/examples/views/js_commands/js_commands.html +++ b/examples/views/js_commands/js_commands.html @@ -1,3 +1,12 @@ + +
PyView JS commands support is a work in progress.
@@ -6,23 +15,80 @@JS Commands let you update the DOM without making a trip to the server.
JS Commands let you update the DOM without making a trip to the server.
+ +JS Commands let you update the DOM without making a trip to the server.
+ Dispatch lets you send custom javascript events on the client, which you can listen to
+ using window.addEventListener
.
+
+ This example sends a "copy-to-clipboard" event when the button is clicked. +
++ It also demonstrates how to chain multiple JS commands together - this example adds a class to the button + when the copy-to-clipboard event is dispatched. +
+{{ js.dispatch("copy-to-clipboard", "#copy-text") }}+ + + +
+ Push lets you push a new event to your view, similar to phx-click
.
+
+ This example increments a counter when the button is clicked. +
+
+ This can be useful if you want to chain the push event with other JS commands, like a transition. This example
+ uses the js.transition
command to add a bounce animation to the counter when it is incremented.
+
+ Counter | {{value}} +
+ + ++ Focus lets you focus an element on the page. +
+
+ The first button uses js.focus("#email")
to focus the email input.
+
+ The second button uses js.focus_first("#focus-form")
to focus the first input in the form.
+