feat: Change the highlight in article page on scrolling (#8330)
This commit is contained in:
@@ -2,27 +2,21 @@
|
|||||||
<div class="hidden lg:block flex-1 py-6 scroll-mt-24 pl-4">
|
<div class="hidden lg:block flex-1 py-6 scroll-mt-24 pl-4">
|
||||||
<div v-if="rows.length > 0" class="sticky top-24 py-2 overflow-auto">
|
<div v-if="rows.length > 0" class="sticky top-24 py-2 overflow-auto">
|
||||||
<nav class="max-w-2xl">
|
<nav class="max-w-2xl">
|
||||||
<h2
|
<ol
|
||||||
id="on-this-page-title"
|
role="list"
|
||||||
class="text-slate-800 pl-6 dark:text-slate-50 font-semibold tracking-wide py-3 leading-7 border-l-2 border-solid border-slate-100 dark:border-slate-800"
|
class="flex flex-col gap-2 text-base border-l-2 border-solid border-slate-100 dark:border-slate-800"
|
||||||
:class="{
|
|
||||||
'!border-slate-400 dark:!border-slate-100': !currentSlug,
|
|
||||||
}"
|
|
||||||
>
|
>
|
||||||
{{ tocHeader }}
|
|
||||||
</h2>
|
|
||||||
<ol role="list" class="text-base">
|
|
||||||
<li
|
<li
|
||||||
v-for="element in rows"
|
v-for="element in rows"
|
||||||
:key="element.slug"
|
:key="element.slug"
|
||||||
class="leading-6 py-2 pl-6 border-l-2 border-solid"
|
class="leading-6 border-l-2 relative -left-0.5 border-solid"
|
||||||
:class="elementBorderStyles(element)"
|
:class="elementBorderStyles(element)"
|
||||||
>
|
>
|
||||||
<p :class="getClassName(element)">
|
<p class="py-2 px-4" :class="getClassName(element)">
|
||||||
<a
|
<a
|
||||||
:href="`#${element.slug}`"
|
:href="`#${element.slug}`"
|
||||||
data-turbolinks="false"
|
data-turbolinks="false"
|
||||||
class="text-base cursor-pointer"
|
class="font-medium text-sm tracking-[0.28px] cursor-pointer"
|
||||||
:class="elementTextStyles(element)"
|
:class="elementTextStyles(element)"
|
||||||
>
|
>
|
||||||
{{ element.title }}
|
{{ element.title }}
|
||||||
@@ -45,12 +39,10 @@ export default {
|
|||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
currentSlug: window.location?.hash?.substring(1) || '',
|
currentSlug: window.location?.hash?.substring(1) || '',
|
||||||
|
intersectionObserver: null,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
computed: {
|
computed: {
|
||||||
tocHeader() {
|
|
||||||
return window.portalConfig.tocHeader;
|
|
||||||
},
|
|
||||||
h1Count() {
|
h1Count() {
|
||||||
return this.rows.filter(el => el.tag === 'h1').length;
|
return this.rows.filter(el => el.tag === 'h1').length;
|
||||||
},
|
},
|
||||||
@@ -59,10 +51,14 @@ export default {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
mounted() {
|
mounted() {
|
||||||
|
this.initializeIntersectionObserver();
|
||||||
window.addEventListener('hashchange', this.onURLHashChange);
|
window.addEventListener('hashchange', this.onURLHashChange);
|
||||||
},
|
},
|
||||||
beforeDestroy() {
|
beforeDestroy() {
|
||||||
window.removeEventListener('hashchange', this.onURLHashChange);
|
window.removeEventListener('hashchange', this.onURLHashChange);
|
||||||
|
if (this.intersectionObserver) {
|
||||||
|
this.intersectionObserver.disconnect();
|
||||||
|
}
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
getClassName(el) {
|
getClassName(el) {
|
||||||
@@ -85,18 +81,32 @@ export default {
|
|||||||
|
|
||||||
return '';
|
return '';
|
||||||
},
|
},
|
||||||
|
initializeIntersectionObserver() {
|
||||||
|
this.intersectionObserver = new IntersectionObserver(
|
||||||
|
entries => {
|
||||||
|
const currentSection = entries.find(entry => entry.isIntersecting);
|
||||||
|
if (currentSection) {
|
||||||
|
this.currentSlug = currentSection.target.id;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
threshold: 0.25,
|
||||||
|
rootMargin: '0px 0px -20% 0px',
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
this.rows.forEach(el => {
|
||||||
|
const sectionElement = document.getElementById(el.slug);
|
||||||
|
if (!sectionElement) return;
|
||||||
|
this.intersectionObserver.observe(sectionElement);
|
||||||
|
});
|
||||||
|
},
|
||||||
onURLHashChange() {
|
onURLHashChange() {
|
||||||
this.currentSlug = window.location?.hash?.substring(1) || '';
|
this.currentSlug = window.location?.hash?.substring(1) || '';
|
||||||
},
|
},
|
||||||
isElementActive(el) {
|
isElementActive(el) {
|
||||||
return this.currentSlug === el.slug;
|
return this.currentSlug === el.slug;
|
||||||
},
|
},
|
||||||
tocHeaderBorderStyles() {
|
|
||||||
if (this.currentSlug === '') {
|
|
||||||
return 'border-slate-400 dark:border-slate-100';
|
|
||||||
}
|
|
||||||
return 'border-slate-100 dark:border-slate-800';
|
|
||||||
},
|
|
||||||
elementBorderStyles(el) {
|
elementBorderStyles(el) {
|
||||||
if (this.isElementActive(el)) {
|
if (this.isElementActive(el)) {
|
||||||
return 'border-slate-400 dark:border-slate-50 transition-colors duration-200';
|
return 'border-slate-400 dark:border-slate-50 transition-colors duration-200';
|
||||||
@@ -105,7 +115,7 @@ export default {
|
|||||||
},
|
},
|
||||||
elementTextStyles(el) {
|
elementTextStyles(el) {
|
||||||
if (this.isElementActive(el)) {
|
if (this.isElementActive(el)) {
|
||||||
return 'font-semibold text-slate-900 dark:text-slate-25';
|
return 'text-slate-900 dark:text-slate-25 transition-colors duration-200';
|
||||||
}
|
}
|
||||||
return 'text-slate-700 dark:text-slate-100';
|
return 'text-slate-700 dark:text-slate-100';
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -14,9 +14,9 @@
|
|||||||
<div class="grid grid-cols-1 md:grid-cols-2 gap-x-2 gap-y-2 gap-2">
|
<div class="grid grid-cols-1 md:grid-cols-2 gap-x-2 gap-y-2 gap-2">
|
||||||
<% featured_articles.each do |article| %>
|
<% featured_articles.each do |article| %>
|
||||||
<a class="text-slate-700 dark:text-slate-100 leading-7" href="<%= generate_article_link(portal.slug, article.slug, @theme) %>">
|
<a class="text-slate-700 dark:text-slate-100 leading-7" href="<%= generate_article_link(portal.slug, article.slug, @theme) %>">
|
||||||
<div class="flex justify-between items-center py-1 px-2 rounded-lg gap-3 hover:bg-slate-50 dark:hover:bg-slate-800">
|
<div class="flex justify-between items-start py-1 px-2 rounded-lg gap-3 hover:bg-slate-50 dark:hover:bg-slate-800">
|
||||||
<%= article.title %>
|
<%= article.title %>
|
||||||
<span class="flex items-center font-normal">
|
<span class="flex items-center font-normal mt-1.5">
|
||||||
<%= render partial: 'icons/chevron-right' %>
|
<%= render partial: 'icons/chevron-right' %>
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user