All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.raquo.laminar.keys.HtmlAttr.scala Maven / Gradle / Ivy

The newest version!
package com.raquo.laminar.keys

import com.raquo.airstream.core.Source
import com.raquo.laminar.DomApi
import com.raquo.laminar.api.L.{optionToSetter, HtmlElement}
import com.raquo.laminar.codecs.Codec
import com.raquo.laminar.modifiers.{KeySetter, KeyUpdater, Setter}
import com.raquo.laminar.modifiers.KeySetter.HtmlAttrSetter
import com.raquo.laminar.modifiers.KeyUpdater.HtmlAttrUpdater

/**
  * This class represents an HTML Element Attribute. Meaning the key that can be set, not the whole a key-value pair.
  *
  * @tparam V type of values that this Attribute can be set to
  */
class HtmlAttr[V](
  override val name: String,
  val codec: Codec[V, String]
) extends Key {

  @inline def apply(value: V): HtmlAttrSetter[V] = {
    this := value
  }

  def maybe(value: Option[V]): Setter[HtmlElement] = {
    optionToSetter(value.map(v => this := v))
  }

  def :=(value: V): HtmlAttrSetter[V] = {
    new KeySetter[HtmlAttr[V], V, HtmlElement](this, value, DomApi.setHtmlAttribute)
  }

  def <--(values: Source[V]): HtmlAttrUpdater[V] = {
    new KeyUpdater[HtmlElement, HtmlAttr[V], V](
      key = this,
      values = values.toObservable,
      update = (el, v, _) => DomApi.setHtmlAttribute(el, this, v)
    )
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy