Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Add language direction picker to Storybook (#346)

sarayourfriendd2b9868387ab
diff --git a/.eslintignore b/.eslintignore
index 849ddff3b..2bf925996 100644
--- a/.eslintignore
+++ b/.eslintignore
@@ -1 +1,4 @@
dist/
+# eslint by default ignores hidden folders so we need to "un-ignore" it
+# https://github.com/eslint/eslint/issues/8429#issuecomment-292789858
+!.storybook
diff --git a/.storybook/decorators/with-rtl.js b/.storybook/decorators/with-rtl.js
new file mode 100644
index 000000000..6f1f8f8af
--- /dev/null
+++ b/.storybook/decorators/with-rtl.js
@@ -0,0 +1,31 @@
+import Vue from 'vue'
+
+import { ref, watch, useContext } from '@nuxtjs/composition-api'
+import { useEffect } from '@storybook/client-api'
+
+const languageDirection = Vue.observable({ value: 'ltr' })
+
+export const WithRTL = (story, context) => {
+ useEffect(() => {
+ languageDirection.value = context.globals.languageDirection
+ }, [context.globals.languageDirection])
+
+ return {
+ template: `<div ref="element"><story /></div>`,
+ components: { story },
+ setup() {
+ const element = ref()
+ const { i18n } = useContext()
+ watch(languageDirection, (direction) => {
+ i18n.localeProperties.dir = direction.value
+ if (element.value) {
+ element.value.ownerDocument.documentElement.setAttribute(
+ 'dir',
+ direction.value
+ )
+ }
+ })
+ return { element }
+ },
+ }
+}
diff --git a/.storybook/main.js b/.storybook/main.js
new file mode 100644
index 000000000..7eb9e3257
--- /dev/null
+++ b/.storybook/main.js
@@ -0,0 +1,15 @@
+const { nuxifyStorybook } = require('../.nuxt-storybook/storybook/main.js')
+
+module.exports = nuxifyStorybook({
+ webpackFinal(config) {
+ // extend config here
+
+ return config
+ },
+ stories: [
+ // Add your stories here
+ ],
+ addons: [
+ // Add your addons here
+ ],
+})
diff --git a/.storybook/middleware.js b/.storybook/middleware.js
new file mode 100644
index 000000000..ff5d022c4
--- /dev/null
+++ b/.storybook/middleware.js
@@ -0,0 +1 @@
+module.exports = require('../.nuxt-storybook/storybook/middleware.js')
diff --git a/.storybook/preview.js b/.storybook/preview.js
new file mode 100644
index 000000000..4d78a3141
--- /dev/null
+++ b/.storybook/preview.js
@@ -0,0 +1,25 @@
+export * from '~~/.nuxt-storybook/storybook/preview.js'
+import {
+ globalTypes as nuxtGlobalTypes,
+ decorators as nuxtDecorators,
+} from '~~/.nuxt-storybook/storybook/preview.js'
+
+import { WithRTL } from './decorators/with-rtl'
+
+export const globalTypes = {
+ ...nuxtGlobalTypes,
+ languageDirection: {
+ name: 'RTL',
+ description: 'Simulate an RTL language.',
+ defaultValue: 'ltr',
+ toolbar: {
+ icon: 'globe',
+ items: [
+ { value: 'ltr', title: 'LTR' },
+ { value: 'rtl', title: 'RTL' },
+ ],
+ },
+ },
+}
+
+export const decorators = [...nuxtDecorators, WithRTL]
diff --git a/nuxt.config.js b/nuxt.config.js
index ceef587b4..b62c92219 100644
--- a/nuxt.config.js
+++ b/nuxt.config.js
@@ -217,7 +217,7 @@ export default {
options: {
backgrounds: false,
viewport: false,
- toolbars: false,
+ toolbars: true,
},
},
],
diff --git a/package-lock.json b/package-lock.json
index 35820f821..cdb75ccd3 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -8377,6 +8377,7 @@
},
"node_modules/@storybook/vue": {
"version": "6.3.7",
+ "resolved": "https://registry.npmjs.org/@storybook/vue/-/vue-6.3.7.tgz",
"integrity": "sha512-ihr5ufy/9YW63fpzIE0Gd9o7gl9yrEOxHLBJ/nU77+Gro5OxtmwMT+G4sq3aZeg0CRIMdOHUKJrzzqSJsPtp/w==",
"dev": true,
"dependencies": {
@@ -45785,6 +45786,7 @@
},
"@storybook/vue": {
"version": "6.3.7",
+ "resolved": "https://registry.npmjs.org/@storybook/vue/-/vue-6.3.7.tgz",
"integrity": "sha512-ihr5ufy/9YW63fpzIE0Gd9o7gl9yrEOxHLBJ/nU77+Gro5OxtmwMT+G4sq3aZeg0CRIMdOHUKJrzzqSJsPtp/w==",
"dev": true,
"requires": {