Nuxt.js Eコマースプラットフォーム向けのレスポンシブモーダルとユーザー通知の設計

WBOY
リリース: 2024-07-18 16:25:18
オリジナル
322 人が閲覧しました

Designing Responsive Modals and User Notifications for Nuxt.js E-commerce Platform

Check this post in my web notes!

In our previous article, we finished creating our e-commerce store main pages, and now it's time to move further and add some dynamic elements like modals and notifications. First of all modal window, will be useful for product quick view, product management, etc., and the notifications role is crucial because it is the best way to communicate with the user, starting from welcome messages and finishing with sale messages. Yes, as you got from the intro and title our today's article will consist of the following:

  1. Creating Custom Modals in Nuxt.js
  2. Configuring Notification Messages in our E-commerce store

Now that we know how important modals and alerts are, let's study how to integrate them into our Nuxt.js e-commerce application and get into the implementation specifics.

1. Creating Custom Modals in Nuxt.js

We need to clarify that we want to create a reusable modal window so that it can be used in many ways and show different data. For that, we will create a main Modal component with modal-specific styles and items that could be rendered inside our modal component.

First things first, create a Modal.vue file inside the "ui" folder with a container that should blur the background and a modal div inside that will contain dynamically rendered items. Also, we will import the "xmark" icon and make it a button that will close our Modal window.

ログイン後にコピー

Inside our base store, we will add two new variables "modalStatus" with a false value and "modalType" as a String, and add simple actions that will modify these values.

aSetModalStatus(status) { this.modalStatus = status; }, aSetModalType(type) { this.modalType = type; },
ログイン後にコピー

The status variable will control modal render behavior and type will update different modal types.

Now, when we have a modal control system we can add our modal component to the default layout. Do not forget to import our base store.

ログイン後にコピー

Great, our reusable modal component is ready, now if we want to show the modal we simply need to update the modalStatus value inside the base store.

Also, you can add whatever styles you want to the modal component, but I recommend you to use "Glassmorphism" and it will look stylish.

.container { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.5); z-index: 100; display: flex; justify-content: center; align-items: center; .modal { min-width: 300px; min-height: 300px; background: rgba( 255, 255, 255, 0.9 ); box-shadow: 0 8px 32px 0 rgba( 31, 38, 135, 0.37 ); backdrop-filter: blur( 13px ); -webkit-backdrop-filter: blur( 13px ); border-radius: 10px; border: 1px solid rgba( 255, 255, 255, 0.18 ); padding: 24px; display: flex; flex-direction: column; &__close { display: flex; justify-content: flex-end; width: 100%; &--btn { border: none; background-color: transparent; cursor: pointer; outline: none; display: flex; align-items: center; } &--icon { color: #6c757d; font-size: 20px; } } } }
ログイン後にコピー

We did a great job over here, but I'm offering you to continue and create the first component that will use our modal - "Product Preview", which will be available after the preview button clicks on any product card. Let's create a new Vue file inside the "common" folder and add some HTML/CSS elements:

ログイン後にコピー

Then import this component into Modal.vue file and insert it into the content div.

ログイン後にコピー

Now, we only need to add a function like "showPreview" to our card so that after the user clicks, that function will set the modal type to "product-preview" and the modal status to "true". That's it, we have a reusable modal and product preview functionality.
modal result
Okay, we need to move on and add alert messages to our Nuxt.js project.

2. Configuring Notification Messages in our E-commerce store

Okay, we can add something like a modal change its styles, and call it alerts, sounds like a simple and fast solution. But what if there will be triggered two or more alerts at the same time (for example "welcome" message and "your wishlist is not empty" message)? To solve this problem we will create an alert container that will be expanding as much as the number of incoming messages.

First of all, we will add alert settings to our store, similar to what we did with the modal part. Add the "notificationsList" array to the state part, and add notification actions that will push and remove the notification from the list. Each notification has a unique ID and shows status, type, message, and delay (amount of time that our message will be visible).

aSetNotification(payload) { const notification = { id: uuidv4(), show: true, type: payload.type, msg: payload.msg, delay: payload.delay ? payload.delay : 7000, } this.notificationsList.push(notification); }, aRemoveNotification(id) { const indexToRemove = this.notificationsList.findIndex((element) => element.id === id); if (indexToRemove !== -1) { this.notificationsList.splice(indexToRemove, 1); } },
ログイン後にコピー

Next move, we need to create a "Notification" component that will render a message itself, depending on the message type. Also, we need to use delay time and remove messages automatically.

 
ログイン後にコピー

You can style your messages in any way you want, I'll as usual use "Glassmorphism" and add some color.

.alert-success { background: rgba( 114, 255, 136, 0.45 ); box-shadow: 0 8px 32px 0 rgba( 31, 38, 135, 0.37 ); backdrop-filter: blur( 10px ); -webkit-backdrop-filter: blur( 10px ); border-radius: 10px; border: 1px solid rgba( 255, 255, 255, 0.18 ); }
ログイン後にコピー

And final touch, we need to create our messages container and add that container into the app.vue file.

The notification container will simply render an array of our list that is stored in the base file.

 
ログイン後にコピー

Do not forget to add "Notification Container" to the main app.vue file.

ログイン後にコピー

That's it, we can restart our dev server and trigger notification messages a few times, just to check the result.
Nuxt js alert result
With the integration of modals and notifications, our Nuxt.js e-commerce application has become more dynamic, interactive, and user-friendly. These elements contribute to an enhanced shopping experience, enabling our customers to preview products and stay informed throughout their journey.

As we continue to build and improve our e-commerce platform, we can further expand the functionality of modals and notifications, tailoring them to specific use cases and ensuring a seamless and engaging experience for our users.

If you would need a source code for this tutorial you can get it here.

以上がNuxt.js Eコマースプラットフォーム向けのレスポンシブモーダルとユーザー通知の設計の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ソース:dev.to
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!