Skip to content

Commit

Permalink
Navigation drawer examples (#380)
Browse files Browse the repository at this point in the history
* Basic navigation drawer examples

* Add previews

* Fix merge issue

* Apply Spotless

* rearrange functions

* Narrowing the examples to just the example with nested items

* Apply Spotless

* refactoring as dismissable drawer

* Fixing imports

* refactor, new region tags

* Renaming functions

* Apply Spotless

* Add horizontal padding to the drawer content

* Apply Spotless

* Make drawer content scrollable to make it work on small screens / landscape

---------

Co-authored-by: jakeroseman <[email protected]>
Co-authored-by: Jolanda Verhoef <[email protected]>
  • Loading branch information
3 people authored Oct 14, 2024
1 parent 5545f37 commit 90b8500
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.example.compose.snippets

import NavigationDrawerExamples
import android.os.Bundle
import android.os.StrictMode
import androidx.activity.ComponentActivity
Expand Down Expand Up @@ -117,6 +118,7 @@ class SnippetsActivity : ComponentActivity() {
TopComponentsDestination.CarouselExamples -> CarouselExamples()
TopComponentsDestination.MenusExample -> MenusExamples()
TopComponentsDestination.TooltipExamples -> TooltipExamples()
TopComponentsDestination.NavigationDrawerExamples -> NavigationDrawerExamples()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
* Copyright 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.outlined.Help
import androidx.compose.material.icons.filled.Menu
import androidx.compose.material.icons.outlined.Settings
import androidx.compose.material3.DrawerValue
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.ModalDrawerSheet
import androidx.compose.material3.ModalNavigationDrawer
import androidx.compose.material3.NavigationDrawerItem
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.rememberDrawerState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.launch

@Composable
fun NavigationDrawerExamples() {
// Add more examples here in future if necessary.

DetailedDrawerExample { innerPadding ->
Box(modifier = Modifier.padding(innerPadding)) {
Text(
"Swipe from left edge or use menu icon to open the dismissible drawer",
modifier = Modifier.padding(16.dp)
)
}
}
}

@OptIn(ExperimentalMaterial3Api::class)
// [START android_compose_components_detaileddrawerexample]
@Composable
fun DetailedDrawerExample(
content: @Composable (PaddingValues) -> Unit
) {
val drawerState = rememberDrawerState(initialValue = DrawerValue.Closed)
val scope = rememberCoroutineScope()

ModalNavigationDrawer(
drawerContent = {
ModalDrawerSheet {
Column(
modifier = Modifier.padding(horizontal = 16.dp)
.verticalScroll(rememberScrollState())
) {
Spacer(Modifier.height(12.dp))
Text("Drawer Title", modifier = Modifier.padding(16.dp), style = MaterialTheme.typography.titleLarge)
HorizontalDivider()

Text("Section 1", modifier = Modifier.padding(16.dp), style = MaterialTheme.typography.titleMedium)
NavigationDrawerItem(
label = { Text("Item 1") },
selected = false,
onClick = { /* Handle click */ }
)
NavigationDrawerItem(
label = { Text("Item 2") },
selected = false,
onClick = { /* Handle click */ }
)

HorizontalDivider(modifier = Modifier.padding(vertical = 8.dp))

Text("Section 2", modifier = Modifier.padding(16.dp), style = MaterialTheme.typography.titleMedium)
NavigationDrawerItem(
label = { Text("Settings") },
selected = false,
icon = { Icon(Icons.Outlined.Settings, contentDescription = null) },
badge = { Text("20") }, // Placeholder
onClick = { /* Handle click */ }
)
NavigationDrawerItem(
label = { Text("Help and feedback") },
selected = false,
icon = { Icon(Icons.AutoMirrored.Outlined.Help, contentDescription = null) },
onClick = { /* Handle click */ },
)
Spacer(Modifier.height(12.dp))
}
}
},
drawerState = drawerState
) {
Scaffold(
topBar = {
TopAppBar(
title = { Text("Navigation Drawer Example") },
navigationIcon = {
IconButton(onClick = {
scope.launch {
if (drawerState.isClosed) {
drawerState.open()
} else {
drawerState.close()
}
}
}) {
Icon(Icons.Default.Menu, contentDescription = "Menu")
}
}
)
}
) { innerPadding ->
content(innerPadding)
}
}
}
// [END android_compose_components_detaileddrawerexample]

@Preview
@Composable
fun DetailedDrawerExamplePreview() {
NavigationDrawerExamples()
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,6 @@ enum class TopComponentsDestination(val route: String, val title: String) {
DatePickerExamples("datePickerExamples", "Date Pickers"),
CarouselExamples("carouselExamples", "Carousel"),
MenusExample("menusExamples", "Menus"),
TooltipExamples("tooltipExamples", "Tooltips")
TooltipExamples("tooltipExamples", "Tooltips"),
NavigationDrawerExamples("navigationDrawerExamples", "Navigation drawer")
}

0 comments on commit 90b8500

Please sign in to comment.