Skip to content

Commit

Permalink
feat: Refactor dynamicFontInjector to handle font loading errors with…
Browse files Browse the repository at this point in the history
… fallback to Roboto

- Added try-catch block for robust error handling during font injection
- Implemented onerror event for font link element to handle loading failures
- Introduced fallback mechanism to apply 'Roboto' font if the requested font fails to load
- Ensured fallback applies if #calendar-widget is not found in the DOM
- Removed all font inherits and replaced with dynamically added css font variable
  • Loading branch information
syam babu committed Oct 11, 2024
1 parent 0f3dc34 commit 928eb44
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 20 deletions.
1 change: 0 additions & 1 deletion src/App.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#calendar-widget {
height: 100%;
font-family: Roboto;
}

#calendar-widget .loader-wrapper {
Expand Down
2 changes: 1 addition & 1 deletion src/components/customCalendar/customCalendar.css
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
}

#calendar-widget > .widget-layout .react-calendar-wrapper * {
font-family: inherit !important;
font-family: var(--calendar-font-family) !important;
}

@media screen and (max-width: 820px) {
Expand Down
2 changes: 1 addition & 1 deletion src/components/search/search.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
border-radius: 68px;
border: 1px solid #b6c1c9;
flex: 1;
font-family: inherit !important;
font-family: var(--calendar-font-family) !important;
font-size: 16px;
font-weight: 400;
line-height: 24px;
Expand Down
18 changes: 12 additions & 6 deletions src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,24 @@ body {
--primary-black: #222732;
}

ul,
li,
p {
#calendar-widget ul,
#calendar-widget li,
#calendar-widget p {
all: unset;
display: block;
}

button {
font-family: inherit;
#calendar-widget button,
#calendar-widget ul,
#calendar-widget li,
#calendar-widget p,
#calendar-widget span,
#calendar-widget input,
#calendar-widget div {
font-family: var(--calendar-font-family);
}

* :focus-visible {
#calendar-widget * :focus-visible {
outline: none;
}

Expand Down
45 changes: 34 additions & 11 deletions src/utils/dynamicStylePropertyInjector.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,42 @@ export const dynamicCssColorInjector = (palette) => {
};

export const dynamicFontInjector = (fontName) => {
if (fontName === 'Roboto') return;
const fallbackFont = 'Roboto';

const fontLink = document.createElement('link');
fontLink.rel = 'stylesheet';
fontLink.href = `https://fonts.googleapis.com/css2?family=${fontName.replace(
/\s+/g,
'+',
)}&display=swap`;
try {
// Inject Google Fonts link
const fontLink = document.createElement('link');
fontLink.rel = 'stylesheet';
fontLink.href = `https://fonts.googleapis.com/css2?family=${fontName.replace(
/\s+/g,
'+',
)}&display=swap`;

document.head.appendChild(fontLink);
fontLink.onerror = () => {
console.error(`Failed to load the font: ${fontName}. Falling back to ${fallbackFont}.`);
setFallbackFont();
};

const calendarWidget = document.getElementById('calendar-widget');
if (calendarWidget) {
calendarWidget.style.fontFamily = `'${fontName}', sans-serif`;
document.head.appendChild(fontLink);

// Apply the font to the #calendar-widget as a CSS variable
const calendarWidget = document.getElementById('calendar-widget');
if (calendarWidget) {
calendarWidget.style.setProperty('--calendar-font-family', `'${fontName}', sans-serif`);
} else {
console.error('Calendar widget not found.');
setFallbackFont();
}
} catch (error) {
console.error('Error during font injection:', error);
setFallbackFont();
}

// Function to apply fallback font
const setFallbackFont = () => {
const calendarWidget = document.getElementById('calendar-widget');
if (calendarWidget) {
calendarWidget.style.setProperty('--calendar-font-family', `'${fallbackFont}', sans-serif`);
}
};
};

0 comments on commit 928eb44

Please sign in to comment.