ダイアログコンポーネントのカスタム実装

モバイルアプリケーション開発において、Vue.jsとVant UIライブラリを採用しています。

よく使われるダイアログとピッカーの2つのコンポーネントについて、デザインガイドラインとの整合性が取れていない場合があります。特にiOS端末におけるボーダー表示に互換性の問題が生じることがあります。

ダイアログコンポーネント

<template>
  <transition name="confirm-fade">
    <div
      v-if="isVisible"
      class="custom-dialog"
      @click.stop="handleClick('cancel')"
      @touchmove.prevent
    >
      <div class="dialog-container" @click.stop>
        <div class="dialog-header">
          <h3>{{title}}</h3>
          <p class="dialog-message">{{message}}</p>
        </div>
        <div class="dialog-actions">
          <button 
            v-if="showCancelButton" 
            class="action-button cancel"
            @click="handleClick('cancel')"
          >
            {{cancelLabel}}
          </button>
          <button 
            class="action-button confirm"
            @click="handleClick('confirm')"
          >
            {{confirmLabel}}
          </button>
        </div>
      </div>
    </div>
  </transition>
</template>
<script>
export default {
  data() {
    return {
      isVisible: false,
      cancelLabel: "キャンセル",
      confirmLabel: "確認",
      showCancelButton: true,
      dialogType: "confirm",
      payload: null,
      title: "確認メッセージ",
      message: "この操作は取り消せません"
    };
  },
  methods: {
    handleClick(action) {
      if (action === 'confirm') {
        this.$emit("onConfirm");
      }
      this.hide();
    },
    show(options) {
      if (typeof options === 'object' && options !== null) {
        this.cancelLabel = options.cancelLabel || "キャンセル";
        this.confirmLabel = options.confirmLabel || "確認";
        this.showCancelButton = options.showCancelButton !== undefined ? options.showCancelButton : true;
        this.dialogType = options.type || "confirm";
        this.payload = options.data || null;
        this.title = options.title || "確認メッセージ";
        this.message = options.message || "この操作は取り消せません";
      }
      this.isVisible = true;
    },
    hide() {
      this.isVisible = false;
      this.title = "確認メッセージ";
      this.cancelLabel = "キャンセル";
      this.confirmLabel = "確認";
      this.showCancelButton = true;
      this.dialogType = "confirm";
      this.payload = null;
    }
  }
};
</script>
<style scoped>
.custom-dialog {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 998;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

.confirm-fade-enter-active {
  animation: fade-in 0.3s;
}
.confirm-fade-enter-active .dialog-container {
  animation: zoom-in 0.3s;
}
.confirm-fade-leave-active {
  animation: fade-out 0.2s;
}

.dialog-container {
  width: 80%;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
  border-radius: 30px;
  z-index: 999;
  user-select: none;
}

.dialog-header {
  padding: 50px 30px 20px 30px;
  text-align: center;
}

.dialog-header h3 {
  font-size: 30px;
  color: #1a1a1a;
  margin-bottom: 30px;
}

.dialog-message {
  font-size: 24px;
  color: #999999;
  line-height: 1.5;
}

.dialog-actions {
  display: flex;
  border-top: 1px solid #dedede;
}

.action-button {
  flex: 1;
  padding: 34px 0;
  text-align: center;
  font-size: 16px;
  background: none;
  border: none;
  outline: none;
}

.action-button.cancel {
  border-right: 1px solid #dedede;
  color: #0064e3;
}

.action-button.confirm {
  color: #0064e3;
}

@keyframes fade-in {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes fade-out {
  from { opacity: 1; }
  to { opacity: 0; }
}

@keyframes zoom-in {
  from { transform: scale(0.8); }
  to { transform: scale(1); }
}
</style>

コンポーネントの利用方法

<CustomDialog ref="dialogComponent" @onConfirm="handleDelete"></CustomDialog>

タグ: vue.js Vant UI ダイアログコンポーネント モバイル開発 iOS互換性

5月22日 08:30 投稿