Layout and Structural Components

Explore Element UI Dialog, Drawer, and Menu components, including practical usage patterns and customization.

🎯 Introduction

Element UI’s layout and structural components help you build attractive, practical interfaces. Whether you need dialogs, side drawers, or nested navigation, these components provide a starting point. This article covers Dialog, Drawer, and Menu, from basic usage to customization, explaining their behavior and typical use cases.


🧱 Dialog

Dialogs are overlays commonly used to display information, confirm actions, or submit forms. Element UI’s Dialog component supports flexible content structures and interactions.

🔑 Opening and Closing

A bound Boolean value, usually named visible, controls whether the dialog is displayed.

<template>
    <el-button @click="dialogVisible = true">Open dialog</el-button>
    <el-dialog :visible.sync="dialogVisible" title="Example dialog" @close="handleClose">
        <p>This is the dialog content</p>
    </el-dialog>
</template>

<script>
export default {
    data() {
        return {
            dialogVisible: false,
        };
    },
    methods: {
        handleClose() {
            console.log('Dialog closed');
        },
    },
};
</script>
Binding visible with the .sync modifier keeps the parent component and dialog state in sync, making visibility easy to control.

Dialog content goes in the default slot. Use the footer slot to customize the layout of action buttons and other footer information.

<template>
    <el-dialog :visible.sync="dialogVisible" title="Dialog with a custom footer">
        <p>This is the dialog body</p>

        <template #footer>
            <el-button @click="dialogVisible = false">Cancel</el-button>
            <el-button type="primary" @click="submitForm">Confirm</el-button>
        </template>
    </el-dialog>
</template>

<script>
export default {
    data() {
        return {
            dialogVisible: false,
        };
    },
    methods: {
        submitForm() {
            alert('Submitted successfully');
            this.dialogVisible = false;
        },
    },
};
</script>
Use the #footer slot to customize the dialog’s footer actions for your application.

🚪 Drawer

A drawer is a container that slides in from a page edge. It is useful for action panels or supplementary information and keeps the current page visible.

💡 Sliding in from the Side

Drawers open from the right by default, but can also slide in from the left, top, or bottom.

<template>
    <el-button @click="drawerVisible = true">Open drawer</el-button>
    <el-drawer :visible.sync="drawerVisible" title="Example drawer" direction="ltr">
        <p>This is the drawer content</p>
    </el-drawer>
</template>

<script>
export default {
    data() {
        return {
            drawerVisible: false,
        };
    },
};
</script>

📐 Size, Direction, and Nesting

  • size accepts pixel values or percentages
  • direction supports four directions: ltr, rtl, ttb, and btt
  • Drawers can be nested for workflows that require multiple panels.
When nesting drawers, consider their closing order so users do not lose track of the panels.

Menus guide users between content or pages. Element UI’s Menu supports multiple levels and router integration for a range of navigation needs.

Menu supports vertical mode, typically used in sidebars, and horizontal mode, typically used for top navigation. Set default-active to specify the initially active item.

<template>
    <el-menu default-active="2" mode="horizontal" @select="handleSelect">
        <el-menu-item index="1">Home</el-menu-item>
        <el-menu-item index="2">Documentation</el-menu-item>
        <el-menu-item index="3">About us</el-menu-item>
    </el-menu>
</template>

<script>
export default {
    methods: {
        handleSelect(index) {
            alert(`Selected menu item: ${index}`);
        },
    },
};
</script>

🏗️ Nested Submenus and Router Integration

Use el-submenu for nested menus at any depth. With Vue Router (2.0), listen for the menu’s select event to navigate to a route.

<template>
    <el-menu default-active="1-1" class="el-menu-vertical" mode="vertical" @select="handleSelect">
        <el-submenu index="1">
            <template #title>Products</template>
            <el-menu-item index="1-1">Product 1</el-menu-item>
            <el-menu-item index="1-2">Product 2</el-menu-item>
        </el-submenu>
        <el-submenu index="2">
            <template #title>Solutions</template>
            <el-menu-item index="2-1">Solution 1</el-menu-item>
            <el-menu-item index="2-2">Solution 2</el-menu-item>
        </el-submenu>
    </el-menu>
</template>

<script>
export default {
    methods: {
        handleSelect(url) {
            this.$router.push(url);
        },
    },
};
</script>
Give each item a unique index that matches its route path or identifier to simplify navigation and state management.

❓ Questions and Answers

Q1: How do I run code when a Dialog closes?

Listen for @close to run cleanup or feedback logic, such as clearing a form or notifying the user.

Q2: How do I make a Drawer open from the left?

Set its direction attribute to 'ltr'.

Q3: How do I navigate to a route when a menu item is clicked?

Listen for @select and call Vue Router’s this.$router.push().


🧪 Exercises

Exercise 1

Create a Dialog with Confirm and Cancel buttons. Confirm should display a message, and Cancel should close the dialog.

Template code:

<template>
    <el-button @click="dialogVisible = true">Open dialog</el-button>
    <el-dialog :visible.sync="dialogVisible" title="Practice dialog">
        <p>Please confirm your action</p>

        <!-- Write your code here -->
    </el-dialog>
</template>

<script>
export default {
    data() {
        return {
            dialogVisible: false,
        };
    },
    methods: {
        confirm() {
            // Add code to display a message when Confirm is clicked
        },
    },
};
</script>

Exercise 2

Create a drawer that opens from the left and contains another drawer that opens from the right. Give each a size and title.

<template>
    <el-button @click="firstDrawer = true">Open first drawer</el-button>
    <el-drawer title="Left drawer" :visible.sync="firstDrawer" size="40%" direction="ltr">
        <el-button @click="secondDrawer = true">Open right drawer</el-button>

        <!-- Add the second drawer here -->
    </el-drawer>
</template>

<script>
export default {
    data() {
        return {
            firstDrawer: false,
            secondDrawer: false,
        };
    },
};
</script>

🎉 You now know the core usage patterns for Dialog, Drawer, and Menu. Try using them to build your project’s interface!