-
Notifications
You must be signed in to change notification settings - Fork 273
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(framework): add configuration to prevent font loading (#9483)
Previously, default fonts could be prevented from loading using a `<style>` tag with the `data-ui5-font-face` attribute. However, recent changes have standardized style additions using adopted stylesheets, which are widely supported by major browsers. This eliminates the need to check for the existence of style elements for validation purposes. Additionally, frameworks like Nuxt.js have raised concerns about empty attributes in these style elements, making the previous solution less optimal. To address this, we have introduced the `defaultFontLoading` configuration option. This provides a more straightforward approach for users who wish to prevent the default font faces from loading and manage this process themselves. Example usage: ```html <script data-ui5-config type="application/json"> { "defaultFontLoading": false } </script> ``` Setting `"defaultFontLoading": false` will prevent the default font faces from being automatically loaded, allowing users to handle font loading according to their specific requirements. Fixes: #8991
- Loading branch information
Showing
8 changed files
with
110 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,38 @@ | ||
# Custom Fonts | ||
|
||
## The data-ui5-font-face style tag | ||
During boot, the UI5 Web Components framework loads the necessary fonts to achieve the desired design of its components. | ||
|
||
Upon `boot`, the UI5 Web Components framework creates a `<style data-ui5-font-face>` tag in the `<head>` in order to load the necessary fonts. | ||
|
||
For example: | ||
|
||
```html | ||
<style type="text/css" data-ui5-font-face=""> | ||
@font-face { | ||
font-family: "72"; | ||
font-style: normal; | ||
font-weight: 400; | ||
src: local("72"), | ||
url(https://sdk.openui5.org/resources/sap/ui/core/themes/sap_fiori_3/fonts/72-Regular.woff2?ui5-webcomponents) format("woff2"); | ||
} | ||
................ | ||
</style> | ||
``` | ||
**Important:** Notice the `data-ui5-font-face` attribute. It is unique and recognized by UI5 Web Components. | ||
**Important:** These fonts are fetched via network requests. | ||
|
||
## Customizing Fonts | ||
|
||
You might need to customize fonts for several reasons: | ||
- To provide different paths for the fonts (e.g. no public internet connection on the server). | ||
- To provide additional declarations inside `@font-face`. | ||
- To download additional fonts, such as e.g. `72-Light`. | ||
- Not to download any of the default fonts. | ||
There are several reasons why you might need to customize fonts: | ||
- To specify different paths for fonts (e.g., due to restrictions on public internet access). | ||
- To include additional declarations within `@font-face`. | ||
- To download additional fonts, such as `72-Light`. | ||
- To prevent the default fonts from being fetched. | ||
|
||
To achieve this, you can prevent the fetching of default fonts by configuring `setDefaultFontLoading (@ui5/webcomponents-base/dist/config/Fonts.js)` to `false`: | ||
|
||
To do that, just create a `<style type="text/css" data-ui5-font-face="">` tag in the `head` of your HTML page and | ||
provide arbitrary content for it. | ||
```ts | ||
import { getDefaultFontLoading, setDefaultFontLoading } from "@ui5/webcomponents-base/dist/config/Fonts.js"; | ||
|
||
Then, when the UI5 Web Components framework boots, it will detect the existence of this tag by the `data-ui5-font-face` | ||
attribute, and will not create it. The one you provided will be used instead. | ||
setDefaultFontLoading(false); | ||
``` | ||
|
||
## Example | ||
Then, specify the custom font you intend to use. When the UI5 Web Components framework initializes, it will refrain from fetching default fonts and instead use the ones you have provided. | ||
|
||
In order to use the `72-Light` font in your app and have an additional setting (`font-display`), you could add the following markup in the `<head>` of your HTML page: | ||
To use the `72-Light` font in your application and specify the `font-display` setting, you should define the font in your application's styles. | ||
|
||
```html | ||
<style type="text/css" data-ui5-font-face=""> | ||
@font-face { | ||
font-family: "72"; | ||
font-style: normal; | ||
font-weight: 200; | ||
font-display: swap; | ||
src: local("72-Light"), | ||
url(https://sdk.openui5.org/resources/sap/ui/core/themes/sap_fiori_3/fonts/72-Light.woff2?ui5-webcomponents) format("woff2"); | ||
} | ||
</style> | ||
<style type="text/css"> | ||
@font-face { | ||
font-family: "72"; | ||
font-style: normal; | ||
font-weight: 200; | ||
font-display: swap; | ||
src: local("72-Light"), | ||
url(https://sdk.openui5.org/resources/sap/ui/core/themes/sap_fiori_3/fonts/72-Light.woff2?ui5-webcomponents) format("woff2"); | ||
} | ||
</style> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { getDefaultFontLoading as getConfiguredDefaultFontLoading } from "../InitialConfiguration.js"; | ||
|
||
let defaultFontLoading: boolean; | ||
|
||
/** | ||
* Returns if the "defaultFontLoading" configuration is set. | ||
* @public | ||
* @returns { boolean } | ||
*/ | ||
const getDefaultFontLoading = (): boolean => { | ||
if (defaultFontLoading === undefined) { | ||
defaultFontLoading = getConfiguredDefaultFontLoading(); | ||
} | ||
|
||
return defaultFontLoading; | ||
}; | ||
|
||
/** | ||
* Defines the "defaultFontLoading" setting. | ||
* | ||
* - When set to "true" (default), all used font faces are fetched over the network. | ||
* - When set to "false", default font faces are not fetched automatically and must be managed separately. | ||
* @public | ||
* @param { boolean } defaultFontLoadingData | ||
*/ | ||
const setDefaultFontLoading = (defaultFontLoadingData: boolean) => { | ||
defaultFontLoading = defaultFontLoadingData; | ||
}; | ||
|
||
export { | ||
getDefaultFontLoading, | ||
setDefaultFontLoading, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters