<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Anis Rasoul</title>
    <description>The latest articles on Forem by Anis Rasoul (@anisrasoul).</description>
    <link>https://forem.com/anisrasoul</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2560657%2F33247c5a-b76c-40be-b348-25b726188284.jpg</url>
      <title>Forem: Anis Rasoul</title>
      <link>https://forem.com/anisrasoul</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/anisrasoul"/>
    <language>en</language>
    <item>
      <title>How to Dynamically Recognize Mobile Mode Using Pinia in Nuxt.js</title>
      <dc:creator>Anis Rasoul</dc:creator>
      <pubDate>Wed, 05 Feb 2025 23:06:49 +0000</pubDate>
      <link>https://forem.com/anisrasoul/how-to-dynamically-recognize-mobile-mode-using-pinia-in-nuxtjs-2jfp</link>
      <guid>https://forem.com/anisrasoul/how-to-dynamically-recognize-mobile-mode-using-pinia-in-nuxtjs-2jfp</guid>
      <description>&lt;p&gt;In this blog, we’ll walk through how to manage the mobile mode state in a Nuxt 3 application using Pinia, Vue’s state management library.&lt;br&gt;
&lt;strong&gt;1- Install Nuxt.js and Pinia&lt;/strong&gt;&lt;br&gt;
First, if you haven't already, create a new Nuxt 3 project and install the necessary dependencies.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx nuxi init my-nuxt-app
cd my-nuxt-app
npm install
npm install pinia

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2- Create a Store to Manage the Mobile Mode State&lt;/strong&gt;&lt;br&gt;
Next, define a store using Pinia to manage the state of the mobile mode. This store will track whether the app is in mobile mode (based on the window size).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { defineStore } from 'pinia';

export const useResponsiveStore = defineStore('responsive', {
  state: () =&amp;gt; ({
    mobileMode: false,
  }),
  actions: {
    setMobileMode(value) {
      this.mobileMode = value;
    },
  },
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3- Set Up Pinia in Nuxt&lt;/strong&gt;&lt;br&gt;
You need to create a plugin to integrate Pinia into your Nuxt 3 app. This plugin will ensure that the store is available globally.&lt;/p&gt;

&lt;p&gt;Create a new file under plugins/pinia.js:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { createPinia } from 'pinia';

export default defineNuxtPlugin((nuxtApp) =&amp;gt; {
  const pinia = createPinia();
  nuxtApp.vueApp.use(pinia);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, make sure to register this plugin in your nuxt.config.js:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export default {
  plugins: ['~/plugins/pinia.js'],
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;4- Update Mobile Mode Based on Window Size&lt;/strong&gt;&lt;br&gt;
To handle window resizing, use a global layout or component that listens for window size changes and updates the mobile mode state accordingly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;template&amp;gt;
  &amp;lt;div&amp;gt;
    &amp;lt;Navbar /&amp;gt;
    &amp;lt;main&amp;gt;
      &amp;lt;slot /&amp;gt;
    &amp;lt;/main&amp;gt;
    &amp;lt;Footer /&amp;gt;
  &amp;lt;/div&amp;gt;
&amp;lt;/template&amp;gt;

&amp;lt;script setup&amp;gt;
import { onMounted, onUnmounted } from 'vue';
import { useResponsiveStore } from '~/stores/useResponsiveStore';

const responsiveStore = useResponsiveStore();

const handleResize = () =&amp;gt; {
  responsiveStore.setMobileMode(window.innerWidth &amp;lt; 768);
};

onMounted(() =&amp;gt; {
  handleResize();
  window.addEventListener('resize', handleResize);
});

onUnmounted(() =&amp;gt; {
  window.removeEventListener('resize', handleResize);
});
&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;5- Use the Mobile Mode State in a Component&lt;/strong&gt;&lt;br&gt;
Now, let's create a simple component that conditionally renders content based on the mobile mode state.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;template&amp;gt;
  &amp;lt;div&amp;gt;
    &amp;lt;p v-if="mobileMode"&amp;gt;You are in mobile mode.&amp;lt;/p&amp;gt;
    &amp;lt;p v-else&amp;gt;You are in desktop mode.&amp;lt;/p&amp;gt;
  &amp;lt;/div&amp;gt;
&amp;lt;/template&amp;gt;

&amp;lt;script setup&amp;gt;
import { computed } from 'vue';
import { useResponsiveStore } from '~/stores/useResponsiveStore';

const responsiveStore = useResponsiveStore();
const mobileMode = computed(() =&amp;gt; responsiveStore.mobileMode);
&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this setup, your Nuxt 3 app will dynamically adjust based on the user's screen size, and you can easily manage the mobile mode state using Pinia.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>nuxt</category>
      <category>vue</category>
    </item>
    <item>
      <title>Aggregation</title>
      <dc:creator>Anis Rasoul</dc:creator>
      <pubDate>Wed, 08 Jan 2025 16:31:43 +0000</pubDate>
      <link>https://forem.com/anisrasoul/aggregation-2ij3</link>
      <guid>https://forem.com/anisrasoul/aggregation-2ij3</guid>
      <description></description>
    </item>
    <item>
      <title>I18n vue</title>
      <dc:creator>Anis Rasoul</dc:creator>
      <pubDate>Thu, 19 Dec 2024 12:12:40 +0000</pubDate>
      <link>https://forem.com/anisrasoul/i18n-vue-5b8i</link>
      <guid>https://forem.com/anisrasoul/i18n-vue-5b8i</guid>
      <description></description>
      <category>vue</category>
      <category>i18n</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
