Skip to content

Commit

Permalink
Use stimulus to handle data action for copy to clipboard
Browse files Browse the repository at this point in the history
Add aria-labels to buttons

Update tests to include button verification
  • Loading branch information
aaron-collier committed Nov 28, 2023
1 parent 27ace22 commit f2e2d8f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 11 deletions.
5 changes: 5 additions & 0 deletions app/assets/stylesheets/media.scss
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,12 @@
border: none;
}

#sul-embed-purl-link {
width: calc(100% - 2rem);
}

#sul-embed-iframe-code {
width: calc(100% - 2rem);
height: 7rem;
}
}
Expand Down
29 changes: 18 additions & 11 deletions app/components/embed/media/embed_this_form_component.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
<div class='sul-embed-embed-this-form'>
<label for="sul-embed-purl-link" class="form-label" readonly>Share link</label>
<input type="text" name="sul-embed-purl-link" id='sul-embed-purl-link' value="<%= purl_url %>" readonly>
<section data-controller="clipboard">
<label for="sul-embed-purl-link" class="form-label" readonly>Share link</label>
<input type="text" name="sul-embed-purl-link" id='sul-embed-purl-link' data-clipboard-target="value" value="<%= purl_url %>" readonly>
<button class="button copy-to-clipboard" data-action="clipboard#copy" aria-label="Copy purl link to clipboard">
<svg class="MuiSvgIcon-root" focusable="false" aria-hidden="true" viewBox="0 0 24 24">
<path d="M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z"></path>
</svg>
</button>
</section>

<label for='sul-embed-iframe-code' class="form-label">Embed</label>
<textarea id='sul-embed-iframe-code' data-behavior='iframe-code' readonly><%= render IframeComponent.new(viewer:) %></textarea>
<section data-controller="clipboard">
<label for='sul-embed-iframe-code' class="form-label">Embed</label>
<textarea id='sul-embed-iframe-code' data-behavior='iframe-code' data-clipboard-target="value" readonly><%= render IframeComponent.new(viewer:) %></textarea>
<button class="button copy-to-clipboard" data-action="clipboard#copy" aria-label="Copy iframe code to clipboard">
<svg class="MuiSvgIcon-root" focusable="false" aria-hidden="true" viewBox="0 0 24 24">
<path d="M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z"></path>
</svg>
</button>
</section>

<fieldset>
<legend>Select options:</legend>
Expand All @@ -28,10 +42,3 @@
</div>
</fieldset>
</div>

<script>
function copyToClipboard(component) {
const element = document.getElementById(component)
navigator.clipboard.writeText(element.value)
}
</script>
9 changes: 9 additions & 0 deletions app/javascript/src/controllers/clipboard_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Controller } from '@hotwired/stimulus'

export default class extends Controller {
static targets = ['value']

copy () {
navigator.clipboard.writeText(this.valueTarget.value)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@
expect(page).to have_css('input#sul-embed-embed-title[type="checkbox"]')
expect(page).to have_css('input#sul-embed-embed[type="checkbox"]')
expect(page).to have_css('textarea#sul-embed-iframe-code')
expect(page).to have_css('button.copy-to-clipboard')
end
end

0 comments on commit f2e2d8f

Please sign in to comment.