Ahmed Riyadh
12/18/2024, 2:30 PMAhmed Riyadh
12/18/2024, 2:30 PMrememberPageContext
which ships JS code in the statically generated site.
I prefer using the Anchor element (<a>
) instead of depending on JS for the following reasons:
1. Functional in case JS is disabled, this is useful for static sites like landing pages.
2. Accessibility and SEO friendly.
3. Natively supported by the browser with better integration.
It gives a better feeling if small details are a priority, for example, right-clicking on the GitHub
button of kobweb.varabyte.com doesn't show the open link actions in the menu, whereas if it's Anchor element, it shows Open Link in New Tab
(see the images). The browser indicates that it's a general button and not a link button.
Browser integration is a nice feature to have; it's a reason why I prefer Compose HTML over Compose for Web. Are there reasons why Kobweb site doesn't use the anchor element (<a>
)?
Example code with Kobweb Silk button style:
// Set the tab index to -1 to ensure pressing the tab key using the keyboard navigates directly to the button and ignores the anchor element.
Link(path = "<https://kobweb.varabyte.com/>", modifier = Modifier.tabIndex(-1)) {
Button(onClick = { // No op }, colorScheme = ColorSchemes.Blue) {
Text("Kobweb")
}
}
// NOTE: This approach is an example and is not production-ready.
Ahmed Riyadh
12/18/2024, 2:31 PMAhmed Riyadh
12/18/2024, 2:31 PMAhmed Riyadh
12/18/2024, 2:35 PMcom.varabyte.kobweb.silk.components.forms.Button
) which is equivalent to the HTML button element.okarm
12/18/2024, 2:41 PM<a>
for external links. For internal links too, if the perceived distance to the destination in terms of functionality is "far" (for some definition of far)S.
12/18/2024, 2:50 PMLink
and provide it with a variant that's styled to look like a buttonS.
12/18/2024, 2:52 PMS.
12/18/2024, 2:54 PMval ButtonLinkVariant = LinkStyle.addVariant(@Composable { ButtonStyle.toModifier() }) {
anyLink {
Modifier.color(Theme.white)
.textDecorationLine(TextDecorationLine.None)
}
base {
Modifier.padding(topBottom = 9.px, leftRight = 1.2.cssRem)
}
}
I'm using something like this to make Links look like buttonsAhmed Riyadh
12/18/2024, 3:17 PMA
(HTML Anchor), and I'm interested in knowing why the Kobweb site (kobweb.varabyte.com) doesn't use the anchor for a link button.
I'm using something like this to make Links look like buttonsYour solution works great, the navigation and the hovering is equivalent to the Button widget and it still has the benefits of the anchor element.
Another way to center the text while using the current text color (to override the anchor default text color):Modifier.padding(topBottom = 9.px, leftRight = 1.2.cssRem)
val ButtonLinkVariant = LinkStyle.addVariant(@Composable { ButtonStyle.toModifier() }) {
anyLink {
Modifier.color(Color.currentColor)
.textDecorationLine(TextDecorationLine.None)
}
base {
Modifier
.display(DisplayStyle.Grid)
.placeItems(AlignItems.Center, JustifyItems.Center)
.textAlign(TextAlign.Center)
}
}
The preference is to use an opinionated solution for consistency that has strong reasons which is important for long-term open-source projects.S.
12/18/2024, 3:21 PMDavid Herman
12/19/2024, 6:49 PMAhmed Riyadh
12/19/2024, 6:59 PM