Hi Friends, how do we disable dragging in a Kobweb...
# kobweb
s
Hi Friends, how do we disable dragging in a Kobweb app globally? Something like writing:
Copy code
html {
  user-drag: none;
}
d
Use an
@InitSilk
block and then
ctx.stylesheet.registerStyle("html")
s
I tried it but the console says this
This is my code
The complete error message is:
Copy code
Style block declarations cannot contain Modifiers that specify attributes. Only style modifiers are allowed here.

Details:
	CSS rule: "html"
	Attribute(s): "draggable"

Please search your `@InitSilk` code for a line like `ctx.stylesheet.registerStyle("html")` and remove the offending attribute(s).
d
Ah easy fix, use
extraModifier
for attributes
Wait scratch that, I'm thinking of CssStyle
I'll look into this shortly when I get to my desk and send you code that you should use instead. Probably you should just use Kotlin/JS here, you can do it in init silk if you'd like, or inside a LaunchedEffect block inside an app.
s
No problem, it's not urgent at all Take your time Probably just celebrate Christmas for now 🎅 😄 We can look into this afterwards
d
I'm having a fun day! Rain incoming so just family is just relaxing. I wanted to suggest something using modifiers but it's tricky -- the upstream Compose HTML methods set some APIs to internal which I would have loved to suggest to you -- but here's an easy suggestion:
Copy code
val rootHtmlElement = window.document.documentElement as HTMLElement
rootHtmlElement.draggable = false
I wouldn't necessarily do that inside an
InitSilk
block at this point -- technically, that's not a silk thing you're doing at this point -- but maybe inside your
@App
block?
Copy code
@App
@Composable
fun AppEntry(content: @Composable () -> Unit) {
   LaunchedEffect(Unit) {
      val rootHtmlElement = ...
      rootHtmlElement.draggable = false
   }
   ... other stuff ...
}
Actually, I just realize you wrote
user-drag
which I guess doesn't exist but is capturing some idea you're trying to figure out in html / css?
Oh it does exist as a webkit css property? Sorry, looking more into it
Maybe try this and report back?
Copy code
@App
@Composable
fun AppEntry(content: @Composable () -> Unit) {
   LaunchedEffect(Unit) {
     // Disable drag/drop globally
     document.addEventListener("dragstart", { event -> event.preventDefault() })
     document.addEventListener("drop", { event -> event.preventDefault() })
     document.addEventListener("dragover", { event -> event.preventDefault() })
   }
   ... other stuff ...
}
That's drag/drop. You might be asking about disabling text selection though?
If you want to disable text selection too, bringing it all together:
Copy code
@InitSilk
fun init(ctx: InitSilkContext) {
    ctx.stylesheet.registerStyleBase("html") {
        Modifier.userSelect(UserSelect.None)
    }
}

@App
@Composable
fun AppEntry(content: @Composable () -> Unit) {
   LaunchedEffect(Unit) {
     // Disable drag/drop globally
     document.addEventListener("dragstart", { event -> event.preventDefault() })
     document.addEventListener("drop", { event -> event.preventDefault() })
     document.addEventListener("dragover", { event -> event.preventDefault() })
   }
   ... other stuff ...
}
s
Thank you very much for all this effort David! It's just commendable how dedicated you are 🙌 I'm going to try it and report back
You might be asking about disabling text selection though?
Text selection is okay, I just don't want users to be able to drag-and-drop UI components like buttons, cards, etc.
Don't want users to be able to do this
Maybe try this and report back?
This worked as expected Thank you 🙌
d
You're welcome! Thanks for confirming