plugins icon indicating copy to clipboard operation
plugins copied to clipboard

🧩 logseq.provideStyle

Open utterances-bot opened this issue 1 year ago • 1 comments

https://plugins-doc.logseq.com/logseq/provideStyle

utterances-bot avatar Jul 13 '24 22:07 utterances-bot

How to toggle CSS in plugin settings

import { LSPluginBaseInfo } from '@logseq/libs/dist/LSPlugin.user'

const keyCssA = "cssA"

// first load
if(logseq.settings!.configBooleanA === true) provideCssA()

// Changed
logseq.onSettingsChanged(async (newSet: LSPluginBaseInfo['settings'], oldSet: LSPluginBaseInfo['settings']) => {

    if(newSet.configBooleanA === true && oldSet.configBooleanA === false){
   provideCssA()
   } else
       if (newSet.configBooleanA === false && oldSet.configBooleanA === true){
   removeProvideCss(keyCssA)
   }

}

// Set <style>
const provideCssA = () => {
logseq.provideStyle({
key: keyCssA, // A plugin-specific key is added to this keyword.
style: `
/* CSS here */

`})
}

// Remove <style>
const removeProvideCss = (className: string) => {
  const doc = parent.document.head.querySelector(`style[data-injected-style^="${className}"]`) as HTMLStyleElement | null
  if (doc) doc.remove()
}

YU000jp avatar Jul 13 '24 22:07 YU000jp