Skip to content

Commit

Permalink
Avoid re-render glitches
Browse files Browse the repository at this point in the history
When changing between languages, the transition between pages can
cause UI glitches, where text could change font for a brief second.

The fix makes the main content invisible by default, and applies the
'invisible' class just before switching languages. After the main
content has rendered, the 'invisible' class is removed. These changes
effectively remove the UI glitches that were present when switching
languages.
  • Loading branch information
0x1eef committed Oct 23, 2023
1 parent 364d6d4 commit 004f6f0
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/css/_layout.scss
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,7 @@ body .root .content.ar {
font-size: xx-large;
}
}

.invisible {
display: none !important;
}
2 changes: 2 additions & 0 deletions src/js/components/LanguageSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export function LanguageSelect({ locale, path = "" }: Props) {
return `${path}/`;
}
})();
const content = document.querySelector(".content.theme");
content.classList.add("invisible");
location.replace(`/${locale}/${newPath}`);
}}
>
Expand Down
12 changes: 10 additions & 2 deletions src/js/pages/SurahIndex.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from "react";
import React, { useRef, useEffect } from "react";
import ReactDOM from "react-dom/client";
import classNames from "classnames";

Expand All @@ -16,9 +16,17 @@ interface Props {

function SurahIndex({ locale, surahs, t }: Props) {
const [theme, setTheme] = useTheme();
const ref = useRef<HTMLDivElement>();

useEffect(() => {
if (ref.current) {
const div = ref.current;
div.classList.remove("invisible");
}
}, []);

return (
<div className={classNames("content", "theme", theme, locale)}>
<div ref={ref} className={classNames("invisible", "content", "theme", theme, locale)}>
<a href={`/${locale}/`} className="row title">
{t(locale, "TheNobleQuran")}
</a>
Expand Down
12 changes: 10 additions & 2 deletions src/js/pages/SurahStream.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useEffect } from "react";
import React, { useState, useEffect, useRef } from "react";
import ReactDOM from "react-dom/client";
import classNames from "classnames";

Expand Down Expand Up @@ -40,14 +40,22 @@ function SurahStream({ node, recitations, locale, paused, t }: Props) {
const readyToRender = stream.length > 0;
const ayah = stream[stream.length - 1];
const hasCompactLayout = ["ar"].includes(locale);
const ref = useRef<HTMLDivElement>();

useEffect(() => {
if (ref.current) {
const div = ref.current;
div.classList.remove("invisible");
}
}, []);

useEffect(() => {
setEndOfStream(false);
setStream([surah.ayat[0]]);
}, [stream.length === 0]);

return (
<div className={classNames("content", "theme", theme, locale)}>
<div ref={ref} className={classNames("invisible", "content", "theme", theme, locale)}>
{readyToRender && (
<>
<a href={`/${locale}/`} className="row title">
Expand Down

0 comments on commit 004f6f0

Please sign in to comment.