34 lines
1.7 KiB
React
34 lines
1.7 KiB
React
"use client";
|
|
import {useEffect} from 'react';
|
|
import {usePathname} from 'next/navigation';
|
|
export function ContentAccess({selector='main.article-shell .article-content, .demo-prose'}){
|
|
const pathname=usePathname();
|
|
useEffect(()=>{
|
|
let alive=true;const regions=new Set();
|
|
for(const root of document.querySelectorAll(selector)){
|
|
for(const pre of root.querySelectorAll('pre')){
|
|
pre.tabIndex=0;pre.setAttribute('role','region');
|
|
if(!pre.getAttribute('aria-label'))pre.setAttribute('aria-label','Пример кода, прокрутка клавишами со стрелками');
|
|
regions.add(pre);
|
|
}
|
|
for(const table of root.querySelectorAll('table')){
|
|
let region=table.parentElement;
|
|
if(!region?.matches('.table-scroll,.scroll-region')){
|
|
region=document.createElement('div');region.className='table-scroll scroll-region';table.before(region);region.append(table);
|
|
}
|
|
region.tabIndex=0;region.setAttribute('role','region');
|
|
if(!region.getAttribute('aria-label'))region.setAttribute('aria-label','Таблица, прокрутка клавишами со стрелками');
|
|
regions.add(region);
|
|
}
|
|
}
|
|
const measure=()=>{if(alive)for(const region of regions)region.dataset.overflow=String(region.scrollWidth>region.clientWidth+1);};
|
|
const observer=typeof ResizeObserver==='function'?new ResizeObserver(measure):null;
|
|
for(const region of regions)observer?.observe(region);
|
|
measure();window.addEventListener('resize',measure);
|
|
document.addEventListener('load',measure,true);
|
|
document.fonts?.ready.then(measure);
|
|
return()=>{alive=false;observer?.disconnect();window.removeEventListener('resize',measure);document.removeEventListener('load',measure,true);};
|
|
},[pathname,selector]);
|
|
return null;
|
|
}
|