Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add importCss helper for importing 3rd party styles into layers #632

Merged
merged 2 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4300,24 +4300,19 @@ layer.
For example, let's say you are fighting with a third party library whose styles are a bit too aggressive and are
interfering with your own styles.

First, inside your build script, import the stylesheet using
an [`@import` directive](https://developer.mozilla.org/en-US/docs/Web/CSS/@import):
First, inside your build script, import the stylesheet using Kobweb's `importCss` function, which internally uses the
CSS [`@import` at-rule](https://developer.mozilla.org/en-US/docs/Web/CSS/@import):

```kotlin
// BEFORE
kobweb.app.index.head.add {
link {
rel = "stylesheet"
href = "/highlight.js/styles/dracula.css"
}
link(href = "/highlight.js/styles/dracula.css", rel = "stylesheet")
}

// AFTER
kobweb.app.index.head.add {
style {
unsafe {
raw("@import url(\"/highlight.js/styles/dracula.css\") layer(highlightjs);")
}
importCss("/highlight.js/styles/dracula.css", layerName = "highlightjs")
}
}
```
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.varabyte.kobweb.gradle.core.util

import kotlinx.html.HEAD
import kotlinx.html.STYLE
import kotlinx.html.TagConsumer
import kotlinx.html.stream.createHTML
import kotlinx.html.unsafe

object HtmlUtil {
// Workaround for generating child nodes without the containing <head> tag
Expand All @@ -23,3 +25,41 @@ object HtmlUtil {
fun serializeHeadContents(block: HEAD.() -> Unit): String =
createHTML(prettyPrint = false, xhtmlCompatible = true).headFragment(block)
}

/**
* Adds an [`@import`](https://developer.mozilla.org/en-US/docs/Web/CSS/@import) rule to import a CSS stylesheet,
* optionally loading it into a specific [CSS Cascade layer](https://developer.mozilla.org/en-US/docs/Learn/CSS/Building_blocks/Cascade_layers).
*
* This can be useful when using a third party CSS library whose styles are a bit too aggressive and are interfering
* with your own styles.
*
* For example, replace a `<link>` tag with an `@import` rule:
*
* ```
* kobweb.app.index.head.add {
* // Before
* link(href = "/highlight.js/styles/dracula.css", rel = "stylesheet")
* // After
* style {
* importCss("/highlight.js/styles/dracula.css", layerName = "highlightjs")
* }
* }
* ```
*
* Then, register your new layer in an `@InitSilk` block:
*
* ```
* @InitSilk
* fun initSilk(ctx: InitSilkContext) {
* // Layer(s) referenced in build.gradle.kts
* ctx.stylesheet.cssLayers.add("highlightjs", after = SilkLayer.BASE)
* }
* ```
*
* @param url The URL of the CSS file to import. This can be an external URL or a path to a local `public` resource.
* @param layerName The cascade layer in which to load the stylesheet. **For this to have any effect, the layer
* MUST be registered in an `@InitSilk` block.**
*/
fun STYLE.importCss(url: String, layerName: String? = null) {
unsafe { raw("@import url(\"$url\")${if (layerName != null) " layer($layerName)" else ""};\n") }
}
Loading