Exploring Kobweb:

A New Framework for Compose Web Development

Barret Vogtman
13 min readSep 23, 2023

--

Picture this: You’re an experienced Android Developer with a passion for crafting digital experiences that leave a lasting impression. Eager to expand your horizons, you’ve set your sights on the world of web development. Your tool of choice? The dynamic duo of Kotlin and Compose, a pair that has consistently delivered stunning user interfaces on Android. Armed with this powerful combination, you embarked on a journey to create a website that would not only showcase your business but also exemplify your dedication to cutting-edge technology.

However, as you delved into Compose for Web, you encountered a web of challenges. The familiar ease of building beautiful user interfaces in Android with Compose seemed to meet its match when applied to web development. The responsive nature of web design, the intricacies of HTML and CSS, and the need for seamless interactivity presented hurdles that felt daunting, leaving you searching for a smoother solution.

But here’s where the plot takes an exciting twist. In the midst of this web development adventure, you stumbled upon a promising new ally: Kobweb. This emerging framework holds the promise of unlocking the full potential of Compose for web development, offering a path to harnessing the power of Kotlin and Compose on the web. Join us as we dive into this innovative solution and discover how it could revolutionize your web development journey.

As an experienced Android Developer with a penchant for crafting captivating digital experiences, your decision to venture into web development was a natural progression. Armed with the formidable combination of Kotlin and Jetpack Compose, a duo that had consistently delivered stunning user interfaces, you were eager to extend your creative reach to the web.

However, your initial exploration of web development with Compose for Web unveiled a complex landscape filled with unique challenges. Compose, celebrated for its simplicity and declarative approach, was originally designed for mobile applications but had been extended to target Kotlin/WebAssembly, making it a promising candidate for web development.

Challenges Unveiled

The first significant hurdle you encountered was the transition from mobile to web development. Compose, while versatile, had to adapt to the responsive and versatile nature of web design. The requirement to ensure seamless adaptation to various screen sizes and orientations, a challenge that Compose typically excels in, remained a central consideration as you transitioned to web development.

Another challenge emerged in the form of the integration with HTML and CSS. Compose, initially tailored for mobile, required bridging the gap between its elegant declarative syntax and the structure-driven world of web documents. Ensuring a seamless fusion of Compose’s capabilities with HTML tags and CSS styling became a perplexing task.

Additionally, achieving smooth interactivity — such as handling user interactions or integrating web-specific functionalities — felt less intuitive within Compose for Web, which had been optimized primarily for web applications, rather than the more familiar mobile app interactions.

Despite your determination and expertise as an Android Developer, it became evident that Compose for Web, while promising and tailored for web development, still presented challenges that required innovative solutions.

Intriguingly, this quest for a more streamlined solution led you to a novel framework that promises to bridge the gap and unlock the untapped potential of Compose for web development: Kobweb. Before delving into the depths of this innovative tool, let’s explore how it stands out in addressing the limitations of Compose for Web.

As we embark on this journey to overcome the challenges posed by Compose for Web and fully unleash the potential of Kotlin and Compose for web development, we introduce you to a promising and innovative solution: Kobweb. This emerging framework has been generating excitement within the web development community for its potential to unlock the full capabilities of Compose when crafting web-based user interfaces.

What is Kobweb?

Kobweb represents a groundbreaking approach to web development, seamlessly integrating with Kotlin and Compose to empower developers with a familiar and powerful toolkit. While we’ve explored its features and advantages in depth, let’s now shift our focus to how Kobweb simplifies page creation and routing while introducing some additional noteworthy features.

Simplified Page Creation and Routing:

With Kobweb, crafting web pages is as simple as defining your Composable functions in a file located under the pages package in the jsMain source set. By annotating your Composables with @Page, you effortlessly declare them as accessible web pages. This streamlined approach to page creation simplifies your project structure and enhances code organization.

// jsMain/kotlin/com/mysite/pages/Settings.kt

@Page
@Composable
fun SettingsPage() {
/* ... */
}

The above code would create a page you could then visit at mysite.com/settings.

Moreover, if you need custom routing beyond the generated route, Kobweb offers the routeOverride feature within the @Page annotation. This functionality is particularly useful for dynamic page generation and handling query parameters, providing flexibility while maintaining code clarity.

// Custom route definition using routeOverride
@Page(routeOverride = "/custom-route")
@Composable
fun CustomPage() {
// Your Composable content for the custom page
}

Additional Features:

Kobweb extends its capabilities by including FontAwesome Icons and Silk UI, enriching your web application with a wide array of visually appealing and functional elements. These pre-integrated resources save you valuable development time and enhance the overall user experience.

Furthermore, Kobweb is built upon Compose HTML, ensuring that you have access to the powerful features and capabilities of Compose while developing web applications. This synergy between Kobweb and Compose HTML empowers you to create stunning and interactive web user interfaces effortlessly.

While Kobweb certainly offers promising advantages, it’s essential to take a balanced view of this emerging framework. In the next section, we’ll explore the pros and cons of Kobweb, providing a comprehensive understanding of its potential benefits and limitations as you embark on your web development journey.

Advantages of Kobweb

Having been introduced to the innovative world of Kobweb and its potential to reshape your web development journey, let’s delve deeper into its advantages, highlighting why it has captured the attention of developers looking to leverage Kotlin and Compose for web-based projects.

  1. Access to Essential Layout Components and State Management Tools: Kobweb offers a rich set of layout components, including Column, Row, Box, and modifier, making it effortless to create intuitive and responsive web user interfaces. But that’s not all. In addition to these layout components, Kobweb also provides robust state management tools like remember. With remember, you can efficiently manage and maintain the state of your web applications, enabling dynamic user experiences that respond seamlessly to user interactions.
  2. Seamless Integration of HTML Tags and Enhanced Styling Control:
    Kobweb excels not only in its seamless integration with HTML tags such as Div, P, A, and H#, but it also empowers you with enhanced control over styling through class names, IDs, and size adjustments in both em and rem units within Compose functions like Row, Column, and Box.
    HTML Tag Integration: Kobweb allows you to effortlessly blend Compose elements with HTML tags, offering a harmonious fusion of functionality and semantics. This integration means you can maintain the structural integrity of your web documents while taking full advantage of Compose’s declarative power to create dynamic and engaging user interfaces.
    Precise Styling Control: In addition to HTML tag integration, Kobweb extends its capabilities to provide precise styling control. You can assign class names and IDs to Compose components, enabling targeted styling through CSS. Moreover, Kobweb permits sizing adjustments in both em and rem units, ensuring pixel-perfect precision in your web layouts.
    With Kobweb’s robust styling options, you have the flexibility to tailor your web applications’ aesthetics to your exact specifications, all while maintaining clean and semantic HTML markup.
  3. Targeting the DOM with JavaScript Powered by Kotlin/JS:
    Kobweb’s ability to target the Document Object Model (DOM) directly with JavaScript is underpinned by the power of Kotlin/JS. This strategic utilization of Kotlin/JS brings forth several advantages that are particularly beneficial, especially for beginners in web development.
    Harnessing Kotlin/JS: Kotlin/JS is a technology that compiles Kotlin code into JavaScript, making it a formidable tool for web development. By harnessing Kotlin/JS, Kobweb allows you to write your web application logic in Kotlin — a language known for its conciseness, safety, and ease of use. This means you can leverage your existing Kotlin skills to build web applications without the need to master JavaScript from scratch.
    A Gentle Learning Curve: For those new to web development, diving directly into the intricacies of JavaScript and WebAssembly (WASM) can be a daunting endeavor. However, Kobweb’s use of Kotlin/JS offers a gentler learning curve. As a developer familiar with Kotlin, you can gradually transition into web development while relying on the strong typing and tooling that Kotlin provides.
    Interoperability with Existing JavaScript Libraries: Kotlin/JS seamlessly integrates with existing JavaScript libraries and frameworks, giving you the flexibility to leverage a vast ecosystem of web development tools. This interoperability means you can tap into popular JavaScript libraries and APIs, extending the capabilities of your Kobweb-powered web applications.
    By targeting the DOM with JavaScript through Kotlin/JS, Kobweb empowers you with a smooth and approachable entry into web development, making it an attractive choice for developers looking to expand their horizons into the world of web-based user interfaces.
  4. ComponentStyle for Advanced Styling and Interactivity:
    Kobweb’s ComponentStyle addition introduces a powerful mechanism for handling styling and interactivity within your web applications. While using modifiers on your components and tags provides inline styling, ComponentStyle takes your web development to the next level by adding to the site’s stylesheet. This opens the door to a host of advanced web features, including hover effects and animations.
    Harnessing the Site’s Stylesheet: ComponentStyle seamlessly integrates with the site’s global stylesheet, allowing you to define styles that affect multiple components and elements across your web application. This level of control not only streamlines your styling process but also ensures consistency and coherence in your design.
    Unlocking Advanced Web Features: With ComponentStyle, you gain access to advanced web features that are typically associated with traditional web development. For instance, you can implement hover effects that respond to user interactions, creating interactive and engaging user interfaces. Additionally, you can bring your web applications to life with animations, enhancing user experience and visual appeal.
    Effortless Management of Complex Styles: ComponentStyle provides an organized and structured way to manage complex styles for your web components. This is particularly beneficial when dealing with intricate layouts or when your web application requires a polished and professional appearance.
    By leveraging ComponentStyle in Kobweb, you can elevate your web development projects to new heights, incorporating advanced styling and interactivity that rival the capabilities of traditional web development approaches.
  5. Access to All Kotlin Classes for Reusable Models:
    Beyond layout components and state management, Kobweb grants you access to the entire spectrum of Kotlin classes, including data classes and enums. This means you can create reusable models and data structures with the same simplicity and power that Kotlin provides. No need to compromise on code quality or maintainability when building your web applications.

Having explored the advantages of Kobweb and how it empowers developers with advanced features for web development, let’s now turn our attention to the limitations and drawbacks that you should be aware of as you consider adopting this innovative framework.

Limitations and Drawbacks of Kobweb:

While Kobweb offers numerous benefits, it’s essential to have a comprehensive understanding of its limitations to make an informed decision when incorporating it into your web development projects.

  1. Learning Curve for Kotlin Beginners: For developers who are new to Kotlin, Kobweb’s integration with Kotlin/JS may pose a learning curve. While Kotlin is known for its simplicity and safety, there may still be a period of adjustment, particularly if you are not already familiar with the language. Beginners to web development might find the combination of learning Kotlin and web development concepts simultaneously to be challenging.
  2. Relatively Limited Documentation: Kobweb is an emerging framework, and as such, it currently lacks extensive documentation, especially in comparison to more established web development tools and frameworks. Developers may find themselves facing hurdles when seeking guidance on specific aspects or troubleshooting issues.
  3. JavaScript Dependency: While Kobweb offers the advantage of targeting the DOM with JavaScript powered by Kotlin/JS, this also means a reliance on JavaScript for web interactivity. Developers who prefer to work with languages other than JavaScript or are less comfortable with it may find this aspect less appealing.
  4. Community and Ecosystem: Kobweb is still in its infancy, which means it has a smaller community and ecosystem compared to well-established web development frameworks. This can result in fewer readily available resources, libraries, and community support.
  5. Continuous Evolution: As with any emerging technology, Kobweb is subject to continuous evolution and refinement. While this can be seen as a positive aspect, it may also mean that developers need to adapt to changes in the framework, which could impact project stability.

Despite these considerations, Kobweb holds immense promise for web development. Let’s now explore some practical use cases and examples to understand how Kobweb can be applied effectively in various scenarios.

Use Cases and Examples

Now that we’ve examined the advantages and limitations of Kobweb, it’s time to see how this innovative framework can be applied in real-world scenarios. In this section, we’ll explore practical use cases and examples that demonstrate the versatility and power of Kobweb in web development. From building responsive user interfaces to crafting interactive web applications, these examples will illustrate how Kobweb can be a valuable asset in your web development toolkit.

Creating a Responsive Layout

In our first example, we’ll dive into the world of responsive web design. Kobweb’s layout components and styling capabilities make it a powerful tool for building web interfaces that adapt seamlessly to different screen sizes. Let’s start by exploring a code block that demonstrates how to create a responsive layout using Kobweb.

@Composable
fun BottomCards(cardInfo: CardInfo) {
Column(
modifier = Modifier.margin(top = 150.px)
) {
Column(
modifier = FlipCardStyle.toModifier(),
horizontalAlignment = Alignment.CenterHorizontally
) {
Column(
modifier = ContentStyle.toModifier()
) {
Column(
modifier = FrontStyle.toModifier(),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.SpaceEvenly
) {
...


}
}
}
}
}
private val commonCardModifier = Modifier
.position(Position.Absolute)
.padding(20.px)
.textAlign(TextAlign.Center)
.borderRadius(20.px)
.styleModifier {
property("backface-visibility", "hidden")
}
.fillMaxSize()
...
val ContentStyle by ComponentStyle.base {
Modifier
.position(Position.Absolute)
.boxShadow(0.px, 0.px, 15.px, color = shadowDark)
.borderRadius(20.px)
.transition(CSSTransition("transform", 1.s))
.styleModifier {
property("transform-style", "preserve-3d")
}
.fillMaxSize()
}
val FrontStyle by ComponentStyle.base {
commonCardModifier
.background(if(colorMode == ColorMode.LIGHT) primaryLight else primaryDark)
.color(if(colorMode==ColorMode.LIGHT) onPrimaryLight else onPrimaryDark)
}
...
val ServiceCtaStyle by ComponentStyle {
base {
Modifier
.backgroundClip(BackgroundClip.PaddingBox)
.backgroundColor(primaryContainerLight)
.border(2.px, LineStyle.Solid, primaryLight)
.borderRadius(2.px)
.color(onPrimaryDark)
.display(DisplayStyle.InlineBlock)
.fontFamily("Raleway", "sans-serif")
.fontSize(17.px)
.fontWeight(400)
.height(36.px)
.letterSpacing(0.5.px)
.lineHeight(36.px)
.padding(0.px, 2.cssRem)
.textDecorationLine(TextDecorationLine.None)
.transition(CSSTransition("all", duration = 200.ms, TransitionTimingFunction.EaseOut))
.margin(bottom = 15.px)
}
hover {
Modifier
.backgroundColor(secondaryLight)
.boxShadow(0.px, 8.px, 17.px, 0.px, shadowLight)
.color(onSecondaryLight)
}
}

The above example shows creating a card that flips on hover. It utilizes the powerful ComponentStyle to give the style and animation that is desired.

While using Columns and Rows is usually preferred by those versed with Android, having access to the HTML tags is very powerful:

@Composable
fun ContactForm(breakpoint: Breakpoint) {

var phoneType by remember { mutableStateOf("") }

Form(
action = "https://",
attrs = ContactFormStyle.toModifier()
.attrsModifier {
attr("method", "POST")
attr("autocomplete", "off")
}
.toAttrs()
) {
SimpleGrid(
modifier = Modifier
.columnGap(10.px)
.alignContent(AlignContent.SpaceEvenly),
numColumns = numColumns(1, lg = 2)
) {
Div{
Label(
forId = "fname",
attrs = FormLabelsStyle.toModifier()
.classNames("form-label")
.toAttrs(),
content = { Text("First Name") }
)
Input(
type = InputType.Text,
attrs = FormInputStyle.toModifier()
.id("fname")
.classNames("form-control")
.attrsModifier {
attr("required", "true")
attr("name", "fname")
}
.toAttrs()
)
}
...
P(
attrs = PContactFormStyle.toModifier()
.gridColumn(1,if(breakpoint >= Breakpoint.LG) 3 else 1)
.toAttrs(),
) {
Span(
attrs = Modifier
.fontSize(FontSize.Large)
.fontWeight(FontWeight.SemiBold)
.toAttrs(),
content = { Text("How can we help?") }
)
Br()
Span(
attrs = Modifier
.fontSize(FontSize.Smaller)
.toAttrs(),
content = { Text("Check all that apply") }
)
}
...
Row(
modifier = Modifier
.padding(top = 10.px)
.gridColumn(1,if(breakpoint >= Breakpoint.LG) 3 else 1)
.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceBetween
) {
Button(
attrs = FormButtonStyle.toModifier()
.classNames("btn", "btn-primary")
.attrsModifier {
attr("type","submit")
}
.toAttrs(),
content = { Text("Submit") }
)
Button(
attrs = FormButtonStyle.toModifier()
.classNames("btn")
.attrsModifier {
attr("type","reset")
}
.toAttrs(),
content = { Text("Reset") }
)
}
}
}
}

As you can see from the above, you can seamlessly switch between Compose layout objects and HTML tags. The above code creates a web form for submitting contact information through an API. The form would appear like this:

Webform created with Kobweb web framework

Conclusion

In this exploration of Kobweb, the emerging framework that brings the power of Kotlin and Compose to web development, we’ve uncovered a promising tool that has the potential to reshape how we create web-based user interfaces. Throughout this article, we’ve highlighted its advantages, limitations, and practical applications.

Kobweb offers several distinct advantages. It provides access to essential layout components, seamless integration of HTML tags, and the ability to target the DOM with JavaScript, all while reducing the dependency on traditional CSS through the innovative ComponentStyle feature. These advantages make it a compelling choice for web developers seeking efficiency and flexibility.

However, it’s important to acknowledge the framework’s limitations, including the learning curve for Kotlin beginners and the relatively limited documentation due to its emergent nature. Additionally, Kobweb relies on JavaScript for web interactivity, which may not align with every developer’s preferences.

What makes Kobweb even more intriguing is its potential within the realm of Kotlin Multiplatform (KMP) projects. With KMP, developers can share code across multiple platforms, including web, mobile, and desktop. Kobweb’s compatibility with Kotlin/JS means that you can seamlessly integrate web components into your KMP project, creating a unified and consistent user experience across different platforms. This synergy between Kobweb and KMP opens up exciting possibilities for cross-platform development.

As Kobweb continues to evolve and gain traction within the web development and KMP communities, it holds immense promise, particularly for those already familiar with Kotlin. The ability to harness Kotlin/JS for web development and the advanced styling capabilities offered by ComponentStyle make it a valuable addition to the web developer’s toolkit, whether you’re building exclusively for the web or as part of a broader multiplatform endeavor.

In conclusion, Kobweb stands as a bridge between the worlds of Kotlin and web development, offering a new way to create responsive, interactive, and visually appealing web applications. As you embark on your web development journey, consider the unique strengths of Kobweb and how it might empower you to craft exceptional web experiences, while also seamlessly fitting into your Kotlin Multiplatform projects.

As the web development and multiplatform landscape evolves, Kobweb represents an exciting step forward, unlocking the untapped potential of Compose for web-based projects and paving the way for a new era of web development and cross-platform innovation.

Thank you for joining us on this journey of discovery, and we look forward to seeing how you leverage Kobweb to create extraordinary web applications and extend its capabilities within your Kotlin Multiplatform projects.

Resources and References

Example of a complete project written in Kotlin with Kobweb:

Where you can find more information about Kobweb:

Kobweb — Home (varabyte.com)

Where you can find the community on discord:

https://discord.gg/ECrNe5p3

--

--

Barret Vogtman

Innovative Android Developer | Tech Enthusiast | Crafting code for exceptional digital experiences | Let's build the future together 🚀