-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
100 additions
and
1 deletion.
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
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,35 @@ | ||
import { loadCSS } from './loadCSS' | ||
|
||
const path = 'https://cdnjs.cloudflare.com/ajax/libs/hover.css/2.3.1/css/hover-min.css' | ||
|
||
describe('loadCSS', () => { | ||
it('should load css', () => { | ||
const { unload, linkTag } = loadCSS(path) | ||
expect(linkTag).toBeInstanceOf(HTMLLinkElement) | ||
expect(linkTag?.href).toBe(path) | ||
expect(linkTag?.rel).toBe('stylesheet') | ||
unload() | ||
}) | ||
it('should load css with media', () => { | ||
const { unload, linkTag } = loadCSS(path, { media: 'print' }) | ||
expect(linkTag).toBeInstanceOf(HTMLLinkElement) | ||
expect(linkTag?.href).toBe(path) | ||
expect(linkTag?.rel).toBe('stylesheet') | ||
expect(linkTag?.media).toBe('print') | ||
unload() | ||
}) | ||
it('should load css with attrs', () => { | ||
const { unload, linkTag } = loadCSS(path, { attrs: { 'data-test': 'test' } }) | ||
expect(linkTag).toBeInstanceOf(HTMLLinkElement) | ||
expect(linkTag?.href).toBe(path) | ||
expect(linkTag?.rel).toBe('stylesheet') | ||
expect(linkTag?.getAttribute('data-test')).toBe('test') | ||
unload() | ||
}) | ||
it('should unload css', () => { | ||
const { unload, linkTag } = loadCSS(path) | ||
expect(linkTag?.href).toBe(path) | ||
unload() | ||
expect(document.querySelector<HTMLLinkElement>(`link[href="${path}"]`)).toBeNull() | ||
}) | ||
}) |
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,54 @@ | ||
interface LoadCSSOptions { | ||
/** | ||
* Media query for styles to apply | ||
* @example 'print', 'all', 'screen and (max-width: 768px)' and etc. | ||
*/ | ||
media?: string | ||
/** | ||
* Add custom attribute to the script tag | ||
* | ||
*/ | ||
attrs?: Record<string, string> | ||
} | ||
|
||
/** | ||
* It loads a CSS file into the page | ||
* @param {string} path - the path to the CSS file | ||
* @param {LoadCSSOptions} [options] - { | ||
* @returns An object with two properties: | ||
* - unload: a function that removes the script tag | ||
* - linkTag: the link tag that was created | ||
*/ | ||
export function loadCSS(path: string, options?: LoadCSSOptions) { | ||
const { | ||
attrs = {}, | ||
media, | ||
} = options || {} | ||
let linkEl = document.querySelector<HTMLLinkElement>(`link[href="${path}"]`) | ||
|
||
if (!linkEl) { | ||
linkEl = document.createElement('link') | ||
linkEl.rel = 'stylesheet' | ||
linkEl.href = path | ||
|
||
if (media) | ||
linkEl.media = media | ||
|
||
Object.entries(attrs).forEach(([name, value]) => linkEl?.setAttribute(name, value)) | ||
|
||
document.head.appendChild(linkEl) | ||
} | ||
|
||
return { | ||
/** remove the script tag */ | ||
unload: () => unload(path), | ||
linkTag: linkEl, | ||
} | ||
} | ||
|
||
/** remove the script tag */ | ||
function unload(path: string) { | ||
const linkEl = document.querySelector(`link[href="${path}"]`) | ||
if (linkEl) | ||
linkEl.remove() | ||
} |
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