Skip to content

Commit

Permalink
add/rm labels on drop
Browse files Browse the repository at this point in the history
also allow a heading
  • Loading branch information
SallyMcGrath committed Jan 23, 2025
1 parent ee048e4 commit 48d2bc6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
19 changes: 18 additions & 1 deletion common-theme/assets/scripts/label-items.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ class LabelItems extends HTMLElement {

render() {
this.shadowRoot.innerHTML = `
<style>
.c-quiz { display: grid;gap: var(--theme-spacing--gutter)}
</style>
<section class="c-quiz">
<slot name="labels"></slot>
<h3>πŸ‘†πŸΎ Drag the labels on to the items πŸ‘‡πŸ½</h3>
<slot name="heading"></slot>
<slot name="content"></slot>
<h4 role="status" aria-live="polite">
<slot name="feedback"></slot>
Expand Down Expand Up @@ -75,6 +78,14 @@ class LabelItems extends HTMLElement {
});
}

makeLabel(labelId) {
// make a label
const label = document.createElement("span");
label.textContent = labelId;
label.classList.add("c-label");
return label;
}

handleDrop(labelId, itemElement) {
const itemId = itemElement.dataset.item;
const isCorrect = this.correctAnswers.get(itemId) === labelId;
Expand All @@ -83,6 +94,12 @@ class LabelItems extends HTMLElement {
itemElement.classList.toggle("is-good", isCorrect);
itemElement.classList.toggle("is-bad", !isCorrect);

// remove old labels
itemElement.querySelectorAll(".c-label").forEach((label) => label.remove());

// add this label
itemElement.appendChild(this.makeLabel(labelId));

// Update feedback slot content
const feedbackSlot = this.shadowRoot.querySelector('slot[name="feedback"]');
const feedback = feedbackSlot.assignedElements()[0];
Expand Down
4 changes: 4 additions & 0 deletions common-theme/assets/styles/04-components/label.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
font: 600 var(--theme-type-size--6) system-ui;
border: 1px solid;
border-radius: 1em;

&:not(:has(.c-label__name)) {
padding: 0.125em 0.5em;
}
&__name {
display: inline-block;
padding: 0.125em 0.5em;
Expand Down
4 changes: 3 additions & 1 deletion common-theme/layouts/shortcodes/label-items.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{{/* example usage: attach a label to delimit each item
{{<label-items
{{<label-items heading="πŸ‘†πŸΎ Drag the labels on to the items πŸ‘‡πŸ½"
>}}
[LABEL=Label-1]Item here [LABEL=Label-2]Item here anything you like can be
multiple lines [LABEL=Label-1] Another item here
{{< /label-items >}}
*/}}
{{ $heading := .Get "heading" | default "πŸ‘†πŸΎ Drag the labels on to the items πŸ‘‡πŸ½" }}
{{/* Split content into items by label, using a similar pattern to TABS and COLUMNS */}}
{{ $content := .Inner | strings.TrimSpace }}
{{ $items := split $content "[LABEL=" }}
Expand Down Expand Up @@ -33,6 +34,7 @@
<button class="c-label" data-label="{{ . | urlize }}">{{ . }}</button>
{{ end }}
</section>
<h3 slot="heading" class="e-heading__5">{{ $heading }}</h3>
<section slot="content" aria-label="Items to label">
{{ range $index, $item := $processedItems }}
<div data-item="{{ add $index 1 }}" data-correct="{{ .label | urlize }}">
Expand Down

0 comments on commit 48d2bc6

Please sign in to comment.