Created
October 16, 2024 17:28
-
-
Save jeremysmithco/f51468b9bf6d6512fbdeee423fed64fe to your computer and use it in GitHub Desktop.
Stimulus @github/session-resume
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= form_with(model: topic_form, url: spaced_topics_path) do |form| %> | |
<div class="mb-4"> | |
<%= form.label :title %> | |
<%= form.text_field :title, data: { session_resume_target: "resumeable" } %> | |
<%= form.error :title %> | |
</div> | |
<div class="mb-2"> | |
<%= form.label :body, class: "sr-only" %> | |
<%= form.markdown_text_area :body, data: { session_resume_target: "resumeable" } %> | |
<%= form.error :body %> | |
</div> | |
<div class="flex justify-between"> | |
<%= form.submit "Save" %> | |
</div> | |
<% end %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8" /> | |
<meta name="viewport" content="width=device-width,initial-scale=1"> | |
<meta name="turbo-prefetch" content="false"> | |
<%= csrf_meta_tags %> | |
<%= csp_meta_tag %> | |
<%= stylesheet_link_tag "application", "data-turbo-track": "reload" %> | |
<%= javascript_include_tag "application", "data-turbo-track": "reload", defer: true %> | |
</head> | |
<body data-controller="session-resume" | |
data-action=" | |
submit@window->session-resume#set:capture | |
turbo:load@window->session-resume#restore | |
turbo:before-visit@window->session-resume#persist | |
pagehide@window->session-resume#persist | |
"> | |
<%= yield %> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Controller } from "@hotwired/stimulus"; | |
import { persistResumableFields, restoreResumableFields, setForm } from "@github/session-resume"; | |
export default class extends Controller { | |
static targets = ["resumeable"]; | |
persist() { | |
if (!this.hasResumeableTarget) return; | |
persistResumableFields(this.pageID, { fields: this.resumeableTargets }); | |
} | |
restore() { | |
restoreResumableFields(this.pageID); | |
} | |
set(event) { | |
setForm(event); | |
} | |
get pageID() { | |
return window.location.pathname; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment