Middle+
How to implement a table component with different border colors in different contexts using BEM and component approach in Vue?
sobes.tech AI
Answer from AI
To implement a table with different border colors in various contexts using BEM and a component-based Vue approach, you can do the following:
- Define a base table component with a block class, for example,
table. - Create modifiers for different contexts, such as
table--primary,table--secondary. - In CSS, describe styles for these modifiers, setting the desired border colors.
- In the Vue component, accept a
contextprop and dynamically add the corresponding class.
Example:
<template>
<table :class="['table', `table--${context}`]">
<thead class="table__head">
<tr class="table__row">
<th class="table__cell">Header</th>
</tr>
</thead>
<tbody class="table__body">
<tr class="table__row">
<td class="table__cell">Data</td>
</tr>
</tbody>
</table>
</template>
<script>
export default {
name: 'Table',
props: {
context: {
type: String,
default: 'primary'
}
}
}
</script>
<style scoped>
.table {
border-collapse: collapse;
}
.table__cell {
border: 1px solid;
padding: 8px;
}
.table--primary .table__cell {
border-color: blue;
}
.table--secondary .table__cell {
border-color: green;
}
</style>
This approach maintains BEM semantics, makes it easy to extend styles for new contexts, and supports component reusability.