Retained tab state using location hash (#161)

This commit is contained in:
Kieran 2024-04-03 11:05:49 -07:00 committed by GitHub
parent 79c61bca4f
commit 3b1c1692fb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 27 additions and 3 deletions

View file

@ -22,6 +22,7 @@ import { Socket } from 'phoenix'
import { LiveSocket } from 'phoenix_live_view'
import topbar from '../vendor/topbar'
import Alpine from 'alpinejs'
import './tabs'
window.Alpine = Alpine
Alpine.start()

20
assets/js/tabs.js Normal file
View file

@ -0,0 +1,20 @@
window.setTabIndex = (index) => {
window.location.hash = `tab-${index}`
return index
}
// The conditionals and currIndex stuff ensures that
// the tab index is always set to 0 if the hash is empty
// AND other hash values are ignored
window.getTabIndex = (currIndex) => {
if (window.location.hash === '' || window.location.hash === '#') {
return 0
}
if (window.location.hash.startsWith('#tab-')) {
return parseInt(window.location.hash.replace('#tab-', ''))
}
return currIndex
}

View file

@ -14,7 +14,8 @@ defmodule PinchflatWeb.CustomComponents.TabComponents do
def tabbed_layout(assigns) do
~H"""
<div
x-data="{ openTab: 0, activeClasses: 'text-meta-5 border-meta-5', inactiveClasses: 'border-transparent' }"
x-data="{ openTab: getTabIndex(0), activeClasses: 'text-meta-5 border-meta-5', inactiveClasses: 'border-transparent' }"
@hashchange.window="openTab = getTabIndex(openTab)"
class="w-full"
>
<header class="flex flex-col md:flex-row md:justify-between border-b border-strokedark">
@ -22,7 +23,7 @@ defmodule PinchflatWeb.CustomComponents.TabComponents do
<a
:for={{tab, idx} <- Enum.with_index(@tab)}
href="#"
@click.prevent={"openTab = #{idx}"}
@click.prevent={"openTab = setTabIndex(#{idx})"}
x-bind:class={"openTab === #{idx} ? activeClasses : inactiveClasses"}
class="border-b-2 py-4 w-full sm:w-fit text-sm font-medium hover:text-meta-5 md:text-base"
>

View file

@ -52,7 +52,9 @@
<:tab title="Sources">
<.table rows={@media_profile.sources} table_class="text-black dark:text-white">
<:col :let={source} label="Name">
<%= source.custom_name || source.collection_name %>
<.subtle_link href={~p"/sources/#{source.id}"}>
<%= source.custom_name || source.collection_name %>
</.subtle_link>
</:col>
<:col :let={source} label="Type"><%= source.collection_type %></:col>
<:col :let={source} label="Should Download?">