Skip to content

Commit

Permalink
chore: Update components with cleaner comments and less dependencies.
Browse files Browse the repository at this point in the history
  • Loading branch information
fourjuaneight authored Jan 7, 2025
1 parent 2100e17 commit 307dbda
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
6 changes: 0 additions & 6 deletions templates/components/cleanHTML.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,7 @@ interface CleanHTMLProps {

/**
* Sanitized HTML; ensures only specified tags attributes pass through.
* @component
*
* @param {string} html raw HTML string
*
* @returns {FunctionComponent} <CleanHTML html={html} />
*/

export const CleanHTML: FunctionComponent<CleanHTMLProps> = ({
html,
}): FunctionComponent => {
Expand Down
13 changes: 13 additions & 0 deletions templates/components/isMobile.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const body = document.querySelector("body");

/**
* Update the body element with a data attribute to indicate if the screen is mobile or not.
*/
const updateBody = (element: Element) => {
window.requestAnimationFrame(() => {
const rect = element.getBoundingClientRect();
Expand All @@ -11,6 +14,12 @@ const updateBody = (element: Element) => {
}
});
};

/**
* Debounce function to limit the number of times a function is called.
* @param ms - number of milliseconds to wait before calling the function.
* @param cb - callback method
*/
const debounce = (ms: number, cb: ResizeObserverCallback) => {
let timer;

Expand All @@ -23,6 +32,10 @@ const debounce = (ms: number, cb: ResizeObserverCallback) => {
};
};

/**
* Check if the browser supports ResizeObserver and if so, use it to detect screen size changes.
* If not, use the resize event listener.
*/
if ("ResizeObserver" in window && body) {
const resizeObserver = new ResizeObserver(
debounce(250, (entries) => {
Expand Down
14 changes: 10 additions & 4 deletions templates/components/sessionState.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { fromJS } from "immutable";

import { IState } from "./types";
interface IState {
[key: string]: any;
}

/**
* Load state from session storage
*/
export const loadState = (): IState | undefined => {
try {
const serializedState: string | null =
Expand All @@ -11,14 +14,17 @@ export const loadState = (): IState | undefined => {
return undefined;
}

return fromJS(JSON.parse(serializedState));
return JSON.parse(serializedState);
} catch (error) {
console.error("Load State Error:", error);

return undefined;
}
};

/**
* Save state to session storage
*/
export const saveState = (state: IState) => {
try {
const serializedState: string = JSON.stringify(state);
Expand Down

0 comments on commit 307dbda

Please sign in to comment.