You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
36 lines
1.0 KiB
36 lines
1.0 KiB
5 years ago
|
/**
|
||
|
* Switch the CSS stylesheet used between 'rtl' and 'ltr'
|
||
4 years ago
|
* @param {('ltr' | 'rtl' | 'auto')} direction - Text direction, either left-to-right (ltr) or right-to-left (rtl)
|
||
4 years ago
|
* @return {Promise<void>}
|
||
5 years ago
|
*/
|
||
|
const switchDirection = async (direction) => {
|
||
|
if (direction === 'auto') {
|
||
4 years ago
|
// eslint-disable-next-line no-param-reassign
|
||
4 years ago
|
direction = 'ltr';
|
||
5 years ago
|
}
|
||
4 years ago
|
let updatedLink;
|
||
5 years ago
|
Array.from(document.getElementsByTagName('link'))
|
||
5 years ago
|
.filter((link) => link.rel === 'stylesheet')
|
||
|
.forEach((link) => {
|
||
5 years ago
|
if (link.title === direction && link.disabled) {
|
||
4 years ago
|
link.disabled = false;
|
||
|
updatedLink = link;
|
||
5 years ago
|
} else if (link.title !== direction && !link.disabled) {
|
||
4 years ago
|
link.disabled = true;
|
||
5 years ago
|
}
|
||
4 years ago
|
});
|
||
5 years ago
|
if (updatedLink) {
|
||
|
return new Promise((resolve, reject) => {
|
||
|
updatedLink.onload = () => {
|
||
4 years ago
|
resolve();
|
||
|
};
|
||
4 years ago
|
updatedLink.onerror = () =>
|
||
4 years ago
|
reject(new Error(`Failed to load '${direction}' stylesheet`));
|
||
|
});
|
||
5 years ago
|
}
|
||
4 years ago
|
|
||
4 years ago
|
return undefined;
|
||
|
};
|
||
5 years ago
|
|
||
4 years ago
|
export default switchDirection;
|