2025-03-30- 改myButton版面

2025-03-30- 改myButton版面

改myButton版面是:

  • 左邊是設定區(控制面板)
  • 右邊是可以新增、顯示你設計好的按鈕們

我們可以這樣改:


✅ 結果畫面會像這樣:

1
2
3
4
+----------------------------+-------------------------+
| 控制面板 | 新增按鈕區 |
|(設定文字、顏色等等) | 按下按鈕 => 顯示右邊 |
+----------------------------+-------------------------+

✅ 修改重點

  1. display: flex 建兩欄版面
  2. 左邊 = 控制面板右邊 = 顯示 MyButton 的地方
  3. 把原本單一 <MyButton> 改成可以新增多個的 v-for
  4. 點擊後新增按鈕,不要 alert,而是 3 秒顯示訊息再消失

✅ 完整整合後程式碼如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<script setup>
import { ref } from 'vue'
import MyButton from './Lesson3_components/MyButton.vue'

const label = ref('自訂按鈕')
const color = ref('#ff6347')
const textColor = ref('#ffffff')
const fontSize = ref(16)
const round = ref(true)
const square = ref(false)

const buttons = ref([]) // 儲存新增的按鈕
const lastClicked = ref('')
let timeout = null

function handleAddButton() {
const newId = Date.now()
buttons.value.push({
id: newId,
label: label.value,
color: color.value,
textColor: textColor.value,
fontSize: fontSize.value,
round: round.value,
square: square.value
})
}

function handleClick(buttonData) {
lastClicked.value = `你剛剛點了:${buttonData.label}`
if (timeout) clearTimeout(timeout)
timeout = setTimeout(() => {
lastClicked.value = ''
}, 3000)
}

// 處理 checkbox 衝突
function onRoundChange() {
if (round.value) square.value = false
}
function onSquareChange() {
if (square.value) round.value = false
}
</script>

<template>
<div class="layout">
<!-- 左邊控制面板 -->
<div class="control-panel">
<h3>按鈕設定</h3>
<label>
按鈕文字:
<input v-model="label" />
</label>

<label>
背景顏色:
<input type="color" v-model="color" />
</label>

<label>
文字顏色:
<input type="color" v-model="textColor" />
</label>

<label>
字體大小:
<input type="range" min="10" max="40" v-model.number="fontSize" />
<span>{{ fontSize }}px</span>
</label>

<label>
圓角樣式:
<input type="checkbox" v-model="round" @change="onRoundChange" />
</label>

<label>
方形樣式:
<input type="checkbox" v-model="square" @change="onSquareChange" />
</label>

<button class="add-button" @click="handleAddButton">➕ 新增按鈕</button>
</div>

<!-- 右邊按鈕區 -->
<div class="button-display">
<h3>按鈕展示區</h3>
<div v-if="lastClicked" class="message">{{ lastClicked }}</div>

<div class="MyButton-wrapper">
<MyButton
v-for="btn in buttons"
:key="btn.id"
:label="btn.label"
:color="btn.color"
:textColor="btn.textColor"
:fontSize="btn.fontSize"
:round="btn.round"
:square="btn.square"
@click="() => handleClick(btn)"
/>
</div>
</div>
</div>
</template>

<style scoped>
.layout {
display: flex;
gap: 2rem;
padding: 2rem;
}

.control-panel {
flex: 1;
display: grid;
grid-template-columns: 1fr;
gap: 1rem;
border: 2px solid #ccc;
border-radius: 10px;
padding: 1.5rem;
background: #f5f5f5;
}

.button-display {
flex: 1;
border: 2px dashed #999;
border-radius: 10px;
padding: 1.5rem;
background: #fff;
}

.MyButton-wrapper {
display: flex;
flex-wrap: wrap;
gap: 1rem;
margin-top: 1rem;
}

.add-button {
padding: 0.5rem 1rem;
background-color: #4caf50;
color: white;
font-weight: bold;
border: none;
border-radius: 8px;
cursor: pointer;
}

.message {
color: #007bff;
font-weight: bold;
margin-bottom: 1rem;
}
</style>

✅ 下一步建議(選擇性功能):

  • 每個按鈕都能刪除:加個 @click.rightx 按鈕
  • 按鈕列表存進 localStorage
  • 匯出按鈕設定成 JSON 檔