|
| 1 | +package com.dbsystel.designsystem.components.link |
| 2 | + |
| 3 | +import androidx.annotation.DrawableRes |
| 4 | +import androidx.compose.animation.animateColorAsState |
| 5 | +import androidx.compose.foundation.clickable |
| 6 | +import androidx.compose.foundation.interaction.MutableInteractionSource |
| 7 | +import androidx.compose.foundation.interaction.collectIsPressedAsState |
| 8 | +import androidx.compose.foundation.layout.Arrangement |
| 9 | +import androidx.compose.foundation.layout.Row |
| 10 | +import androidx.compose.foundation.layout.padding |
| 11 | +import androidx.compose.foundation.layout.size |
| 12 | +import androidx.compose.material3.Icon |
| 13 | +import androidx.compose.material3.Text |
| 14 | +import androidx.compose.runtime.Composable |
| 15 | +import androidx.compose.runtime.ReadOnlyComposable |
| 16 | +import androidx.compose.runtime.getValue |
| 17 | +import androidx.compose.runtime.remember |
| 18 | +import androidx.compose.ui.Alignment |
| 19 | +import androidx.compose.ui.Modifier |
| 20 | +import androidx.compose.ui.draw.alpha |
| 21 | +import androidx.compose.ui.graphics.Color |
| 22 | +import androidx.compose.ui.res.painterResource |
| 23 | +import androidx.compose.ui.text.TextStyle |
| 24 | +import androidx.compose.ui.text.style.TextDecoration |
| 25 | +import androidx.compose.ui.tooling.preview.Preview |
| 26 | +import androidx.compose.ui.unit.Dp |
| 27 | +import androidx.compose.ui.unit.dp |
| 28 | +import com.dbsystel.designsystem.components.link.preview.previewName |
| 29 | +import com.dbsystel.designsystem.foundation.R |
| 30 | +import com.dbsystel.designsystem.foundation.theme.DesignSystemTheme |
| 31 | + |
| 32 | +/** |
| 33 | + * Links are used as navigation elements. They can stand alone, within a sentence or paragraph |
| 34 | + * or directly after the content to which they refer. |
| 35 | + * |
| 36 | + * @param modifier the Modifier to be applied to this link |
| 37 | + * @param text the text to be displayed |
| 38 | + * @param content content type of the link (external or internal link) |
| 39 | + * @param variant visual representation of the link |
| 40 | + * @param size size of the link |
| 41 | + * @param enabled controls the enabled state of this link. When false, this component will not |
| 42 | + * respond to user input, and it will appear visually disabled and disabled to accessibility services. |
| 43 | + * @param showIcon control the visibility of the content type icon |
| 44 | + * @param onClick called when this link is clicked |
| 45 | + * |
| 46 | + * @sample com.dbsystel.designsystem.components.samples.DSLinkSample |
| 47 | + */ |
| 48 | +@Composable |
| 49 | +fun DSLink( |
| 50 | + modifier: Modifier = Modifier, |
| 51 | + text: String, |
| 52 | + content: DSLinkContent = DSLinkContent.INTERNAL, |
| 53 | + variant: DSLinkVariant = DSLinkVariant.ADAPTIVE, |
| 54 | + size: DSLinkSize = DSLinkSize.MEDIUM, |
| 55 | + enabled: Boolean = true, |
| 56 | + showIcon: Boolean = true, |
| 57 | + onClick: () -> Unit, |
| 58 | +) { |
| 59 | + val interactionSource = remember { MutableInteractionSource() } |
| 60 | + val pressed by interactionSource.collectIsPressedAsState() |
| 61 | + |
| 62 | + val linkColor by animateColorAsState( |
| 63 | + targetValue = if (pressed) variant.pressedColor else variant.color, |
| 64 | + label = "LinkColorAnimation", |
| 65 | + ) |
| 66 | + |
| 67 | + Row( |
| 68 | + modifier = Modifier |
| 69 | + .alpha(if (enabled) 1.0f else 0.4f) |
| 70 | + .clickable( |
| 71 | + enabled = enabled, |
| 72 | + onClick = onClick, |
| 73 | + interactionSource = interactionSource, |
| 74 | + indication = null |
| 75 | + ) |
| 76 | + .then(modifier), |
| 77 | + horizontalArrangement = Arrangement.spacedBy(size.paddingH), |
| 78 | + verticalAlignment = Alignment.CenterVertically, |
| 79 | + ) { |
| 80 | + Text( |
| 81 | + text = text, |
| 82 | + textDecoration = TextDecoration.Underline, |
| 83 | + style = size.textStyle, |
| 84 | + color = linkColor, |
| 85 | + ) |
| 86 | + if (showIcon) Icon( |
| 87 | + modifier = Modifier.size(size.iconSize), |
| 88 | + painter = painterResource(content.iconRes), |
| 89 | + contentDescription = content.name, |
| 90 | + tint = linkColor, |
| 91 | + ) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +enum class DSLinkVariant { |
| 96 | + ADAPTIVE, |
| 97 | + BRAND; |
| 98 | + |
| 99 | + internal val color: Color |
| 100 | + @Composable |
| 101 | + @ReadOnlyComposable |
| 102 | + get() = when (this) { |
| 103 | + ADAPTIVE -> DesignSystemTheme.activeColor.Basic.Text.Emphasis100.Default |
| 104 | + BRAND -> DesignSystemTheme.colors.brand.Basic.Text.Emphasis80.Default |
| 105 | + } |
| 106 | + |
| 107 | + internal val pressedColor: Color |
| 108 | + @Composable |
| 109 | + @ReadOnlyComposable |
| 110 | + get() = when (this) { |
| 111 | + ADAPTIVE -> DesignSystemTheme.activeColor.Basic.Text.Emphasis100.Pressed |
| 112 | + BRAND -> DesignSystemTheme.colors.brand.Basic.Text.Emphasis80.Pressed |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +enum class DSLinkSize { |
| 117 | + MEDIUM, |
| 118 | + SMALL; |
| 119 | + |
| 120 | + internal val paddingH: Dp |
| 121 | + @Composable |
| 122 | + @ReadOnlyComposable |
| 123 | + get() = when (this) { |
| 124 | + MEDIUM -> DesignSystemTheme.dimensions.spacing.fixed2xs |
| 125 | + SMALL -> DesignSystemTheme.dimensions.spacing.fixed3xs |
| 126 | + } |
| 127 | + |
| 128 | + internal val iconSize: Dp |
| 129 | + @Composable |
| 130 | + @ReadOnlyComposable |
| 131 | + get() = when (this) { |
| 132 | + MEDIUM -> 24.dp |
| 133 | + SMALL -> 20.dp |
| 134 | + } |
| 135 | + |
| 136 | + internal val textStyle: TextStyle |
| 137 | + @Composable |
| 138 | + @ReadOnlyComposable |
| 139 | + get() = when (this) { |
| 140 | + MEDIUM -> DesignSystemTheme.typography.bodyMd |
| 141 | + SMALL -> DesignSystemTheme.typography.bodySm |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +enum class DSLinkContent { |
| 146 | + INTERNAL, |
| 147 | + EXTERNAL; |
| 148 | + |
| 149 | + internal val iconRes: Int |
| 150 | + @DrawableRes |
| 151 | + get() = when (this) { |
| 152 | + INTERNAL -> R.drawable.ds_ic_arrow_forward |
| 153 | + EXTERNAL -> R.drawable.ds_ic_link_external |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +@Composable |
| 158 | +@Preview( |
| 159 | + showBackground = true, |
| 160 | + group = "Content", |
| 161 | +) |
| 162 | +private fun DSLinkPreview_Content() { |
| 163 | + DesignSystemTheme { |
| 164 | + Row( |
| 165 | + modifier = Modifier |
| 166 | + .padding(12.dp), |
| 167 | + verticalAlignment = Alignment.CenterVertically, |
| 168 | + horizontalArrangement = Arrangement.spacedBy(24.dp), |
| 169 | + ) { |
| 170 | + DSLinkContent.entries.forEach { content -> |
| 171 | + DSLink( |
| 172 | + text = content.previewName, |
| 173 | + content = content, |
| 174 | + onClick = {}, |
| 175 | + ) |
| 176 | + } |
| 177 | + } |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +@Composable |
| 182 | +@Preview( |
| 183 | + showBackground = true, |
| 184 | + group = "Variant", |
| 185 | +) |
| 186 | +private fun DSLinkPreview_Variant() { |
| 187 | + DesignSystemTheme { |
| 188 | + Row( |
| 189 | + modifier = Modifier |
| 190 | + .padding(12.dp), |
| 191 | + verticalAlignment = Alignment.CenterVertically, |
| 192 | + horizontalArrangement = Arrangement.spacedBy(24.dp), |
| 193 | + ) { |
| 194 | + DSLinkVariant.entries.forEach { variant -> |
| 195 | + DSLink( |
| 196 | + text = variant.previewName, |
| 197 | + variant = variant, |
| 198 | + onClick = {}, |
| 199 | + ) |
| 200 | + } |
| 201 | + } |
| 202 | + } |
| 203 | +} |
| 204 | + |
| 205 | +@Composable |
| 206 | +@Preview( |
| 207 | + showBackground = true, |
| 208 | + group = "Disabled", |
| 209 | +) |
| 210 | +private fun DSLinkPreview_Disabled() { |
| 211 | + DesignSystemTheme { |
| 212 | + Row( |
| 213 | + modifier = Modifier |
| 214 | + .padding(12.dp), |
| 215 | + verticalAlignment = Alignment.CenterVertically, |
| 216 | + horizontalArrangement = Arrangement.spacedBy(24.dp), |
| 217 | + ) { |
| 218 | + listOf(false, true).forEach { disabled -> |
| 219 | + DSLink( |
| 220 | + text = if (!disabled) "(Def) False" else "True", |
| 221 | + enabled = !disabled, |
| 222 | + onClick = {}, |
| 223 | + ) |
| 224 | + } |
| 225 | + } |
| 226 | + } |
| 227 | +} |
| 228 | + |
| 229 | +@Composable |
| 230 | +@Preview( |
| 231 | + showBackground = true, |
| 232 | + group = "Size", |
| 233 | +) |
| 234 | +private fun DSLinkPreview_Size() { |
| 235 | + DesignSystemTheme { |
| 236 | + Row( |
| 237 | + modifier = Modifier |
| 238 | + .padding(12.dp), |
| 239 | + verticalAlignment = Alignment.CenterVertically, |
| 240 | + horizontalArrangement = Arrangement.spacedBy(24.dp), |
| 241 | + ) { |
| 242 | + DSLinkSize.entries.forEach { size -> |
| 243 | + DSLink( |
| 244 | + text = size.previewName, |
| 245 | + size = size, |
| 246 | + onClick = {}, |
| 247 | + ) |
| 248 | + } |
| 249 | + } |
| 250 | + } |
| 251 | +} |
| 252 | + |
| 253 | +@Composable |
| 254 | +@Preview( |
| 255 | + showBackground = true, |
| 256 | + group = "ShowIcon", |
| 257 | +) |
| 258 | +private fun DSLinkPreview_ShowIcon() { |
| 259 | + DesignSystemTheme { |
| 260 | + Row( |
| 261 | + modifier = Modifier |
| 262 | + .padding(12.dp), |
| 263 | + verticalAlignment = Alignment.CenterVertically, |
| 264 | + horizontalArrangement = Arrangement.spacedBy(24.dp), |
| 265 | + ) { |
| 266 | + listOf(true, false).forEach { showIcon -> |
| 267 | + DSLink( |
| 268 | + text = if (showIcon) "(Def) True" else "False", |
| 269 | + showIcon = showIcon, |
| 270 | + onClick = {}, |
| 271 | + ) |
| 272 | + } |
| 273 | + } |
| 274 | + } |
| 275 | +} |
0 commit comments