How to add dir attribute to <html> based on the incoming request's cookie?

Viewed 34

I need to set the direction for the selected language.

If it's RTL, I need to set <html dir='rtl'>, otherwise <html dir='ltr'>.

The selected language comes in a cookie in the HTTP Request.

How can I do that in Vite?

1 Answers

You can query the document for the <html>, and set its dir attribute:

const setHtmlLangDir = () => {
  const dir = /* direction of lang from cookie */
  document.querySelector('html').setAttribute('dir', dir);
}

demo

Related