Skip to content

Commit

Permalink
fix: improve resize handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mtwente committed Nov 6, 2024
1 parent bb39273 commit 4435f70
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 20 deletions.
38 changes: 26 additions & 12 deletions _includes/js/switch-view-js.html
Original file line number Diff line number Diff line change
@@ -1,26 +1,40 @@
<script>
const switchView = document.getElementById('switch-view');
const buttons = Array.from(switchView ? switchView.children : []);

function toggleButtonView() {
const switchView = document.getElementById('switch-view');
if (!switchView) return;

switchView.classList.add('btn-group-vertical');
switchView.classList.remove('btn-group');

// Show all buttons except the last one (toggle)
Array.from(switchView.children).forEach((button, index) => {
button.style.display = 'block';

if (index === switchView.children.length - 1) {
button.style.display = 'none';
}
// Hide last button (toggle) from vertical display
buttons.forEach((button, index) => {
button.style.display = index === buttons.length - 1 ? 'none' : 'block';
});
}

window.addEventListener('resize', () => {
if (window.innerWidth > 576) {
const switchView = document.getElementById('switch-view');
// Resizing with debounce
const debounce = (fn, delay) => {
let timeoutId;
return (...args) => {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => fn(...args), delay);
};
};

const handleResize = () => {
if (!switchView) return;

if (window.innerWidth > 576) {
switchView.classList.add('btn-group', 'btn-group-toggle', 'd-flex');
switchView.classList.remove('btn-group-vertical');

buttons.forEach((button) => (button.style.display = ''));
} else {
buttons.forEach((button) => (button.style.display = ''));
}
});
};

window.addEventListener('resize', debounce(handleResize, 250));
</script>
4 changes: 2 additions & 2 deletions _layouts/browse.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
layout: page
custom-foot: js/browse-js.html
---
{% include js/switch-view-js.html %}

{{ content }}

<div class="container-fluid px-0">
Expand Down Expand Up @@ -86,6 +84,8 @@
</div>
</div>

{% include js/switch-view-js.html %}

<div class="h2 mt-2 text-center" id="numberOf"></div>

<div id="loadingIcon" class="text-center">
Expand Down
4 changes: 2 additions & 2 deletions _layouts/cloud.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
# can be used for any metadata field cloud
layout: page
---
{% include js/switch-view-js.html %}

{{ content }}

<div class="container-fluid px-0">
Expand Down Expand Up @@ -58,6 +56,8 @@
</div>
</div>

{% include js/switch-view-js.html %}

{%- comment -%}
Figure out default "Subjects" and "Locations" page cloud values as configured in in "theme.yml",
and set default values for cloud include.
Expand Down
4 changes: 2 additions & 2 deletions _layouts/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
# can be used for any metadata field
layout: page
---
{% include js/switch-view-js.html %}

{{ content }}

{%- comment -%}
Expand Down Expand Up @@ -102,6 +100,8 @@
</div>
</div>

{% include js/switch-view-js.html %}

{% assign list_id = 'list-div-' | append: fields | slugify %}
<div id="{{ list_id }}" class="text-center bg-{{ page.background | default: 'light' }} border rounded p-1"></div>
{% include js/list-js.html id=list_id fields=fields min=min stopwords=stopwords %}
4 changes: 2 additions & 2 deletions _layouts/timeline_edtf.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
{%- assign navYears = navYears | split: ';' -%}
{%- endif -%}

{% include js/switch-view-js.html %}

{{ content }}

<div class="container-fluid px-0">
Expand Down Expand Up @@ -77,6 +75,8 @@
</div>
</div>

{% include js/switch-view-js.html %}

{%- if navYears -%}
<div class="col-12 col-md-auto text-center mb-2 mb-lg-0 ms-md-auto">
<div class="dropdown">
Expand Down

0 comments on commit 4435f70

Please sign in to comment.