aboutsummaryrefslogtreecommitdiffstats
path: root/ui/app/helpers/utils/switch-direction.js
diff options
context:
space:
mode:
Diffstat (limited to 'ui/app/helpers/utils/switch-direction.js')
-rw-r--r--ui/app/helpers/utils/switch-direction.js30
1 files changed, 30 insertions, 0 deletions
diff --git a/ui/app/helpers/utils/switch-direction.js b/ui/app/helpers/utils/switch-direction.js
new file mode 100644
index 000000000..b0cee933e
--- /dev/null
+++ b/ui/app/helpers/utils/switch-direction.js
@@ -0,0 +1,30 @@
+/**
+ * Switch the CSS stylesheet used between 'rtl' and 'ltr'
+ * @param {('ltr' | 'rtl')} direction Text direction, either left-to-right (ltr) or right-to-left (rtl)
+ */
+const switchDirection = async (direction) => {
+ if (direction === 'auto') {
+ direction = 'ltr'
+ }
+ let updatedLink
+ Array.from(document.getElementsByTagName('link'))
+ .filter(link => link.rel === 'stylesheet')
+ .forEach(link => {
+ if (link.title === direction && link.disabled) {
+ link.disabled = false
+ updatedLink = link
+ } else if (link.title !== direction && !link.disabled) {
+ link.disabled = true
+ }
+ })
+ if (updatedLink) {
+ return new Promise((resolve, reject) => {
+ updatedLink.onload = () => {
+ resolve()
+ }
+ updatedLink.onerror = () => reject(new Error(`Failed to load '${direction}' stylesheet`))
+ })
+ }
+}
+
+export default switchDirection