Skip to content

Commit

Permalink
refactor(Assets Path): set bqSVGBasePath in the global window objec…
Browse files Browse the repository at this point in the history
…t only when is available
  • Loading branch information
dgonzalezr committed Aug 29, 2024
1 parent 423dcbd commit f83dd2b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,13 @@ export const ExploreIcons: Story = {
</span>
</div>
<bq-button class="m-be-xxl" appearance="primary" href="https://phosphoricons.com/" target="_blank">
<bq-icon name="binoculars" weight="fill" slot="prefix"></bq-icon>
<bq-icon name="binoculars-fill" slot="prefix"></bq-icon>
Explore all the icons available
<bq-icon class="ms-m" name="caret-right" weight="regular" slot="suffix"></bq-icon>
<bq-icon class="ms-m" name="caret-right" slot="suffix"></bq-icon>
</bq-button>
<!-- Warning block -->
<bq-alert class="m-be-l" type="warning" disable-close open>
<bq-icon name="warning" weight="fill" slot="icon"></bq-icon>
<bq-icon name="warning-fill" slot="icon"></bq-icon>
Please notice
<span slot="body">
The SVG icons will be flipped horizontally when the <code>dir="rtl"</code> attribute is used.
Expand Down
26 changes: 19 additions & 7 deletions packages/beeq/src/shared/utils/assetsPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,23 @@ declare global {
}
}

Object.defineProperty(window, 'bqSVGBasePath', {
configurable: true,
enumerable: false,
writable: true,
});
/**
* Define the `bqSVGBasePath` property on the global window object, but only when the window object is available.
*/
if (typeof window !== 'undefined') {
Object.defineProperty(window, 'bqSVGBasePath', {
configurable: true,
enumerable: false,
writable: true,
});
}

const DATA_BEEQ_ATTRIBUTE = 'data-beeq';
const DEFAULT_SVG_PATH = 'svg';
const scripts = [...document.getElementsByTagName('script')] as HTMLScriptElement[];
const scripts: HTMLScriptElement[] =
typeof document !== 'undefined' && document
? ([...document.getElementsByTagName('script')] as HTMLScriptElement[])
: [];

/**
* Sets the `bqSVGBasePath` in the global window object,
Expand All @@ -31,7 +39,9 @@ const scripts = [...document.getElementsByTagName('script')] as HTMLScriptElemen
* @param path - The new base path to set.
*/
export const setBasePath = (path: string) => {
window.bqSVGBasePath = path;
if (typeof window !== 'undefined') {
window.bqSVGBasePath = path;
}
};

/**
Expand All @@ -41,6 +51,8 @@ export const setBasePath = (path: string) => {
* @returns The full base path including the subpath.
*/
export const getBasePath = (subpath = ''): string => {
if (typeof window === 'undefined') return undefined;

const { bqSVGBasePath } = window;
if (!bqSVGBasePath) {
initializeBasePath();
Expand Down

0 comments on commit f83dd2b

Please sign in to comment.