Skip to content
Permalink
Browse files

Added FontAwesome support

  • Loading branch information...
darkfrog26 committed Jul 10, 2019
1 parent e08ace6 commit 7c893c5c8c6ac1543e4be6749324863f68eec602
@@ -1,5 +1,6 @@
package io.youi

import io.youi.net.URL
import org.scalajs.dom._
import org.scalajs.dom.ext._
import org.scalajs.dom.html.Div
@@ -65,6 +66,14 @@ object dom extends ExtendedElement(None) {
document.head.appendChild(style)
}

def addCSS(url: URL): Unit = {
val link = create[html.Link]("link")
link.href = url.toString
link.setAttribute("as", "style")
link.setAttribute("crossorigin", "crossorigin")
document.head.appendChild(link)
}

implicit class StringExtras(s: String) {
def toElement[T <: Element]: T = {
val temp = document.createElement("div")
@@ -42,6 +42,7 @@ object ClientExampleApplication extends ExampleApplication with ClientApplicatio
val animation: AnimationExample = new AnimationExample
val canvas: CanvasExample = new CanvasExample
val drawable: DrawableExample = new DrawableExample
val fontAwesome: FontAwesomeExample = new FontAwesomeExample
val grid: GridLayoutExample = new GridLayoutExample
val hit: HitTestExample = new HitTestExample
val image: ImageExample = new ImageExample
@@ -0,0 +1,29 @@
package io.youi.example.ui

import io.youi.Color
import io.youi.example.screen.UIExampleScreen
import io.youi.font.{FontAwesome, FontAwesomeView}
import io.youi.net._
import io.youi.style.FontWeight

import scala.concurrent.Future
import scribe.Execution.global

class FontAwesomeExample extends UIExampleScreen {
override def title: String = "Font Awesome Example"
override def path: Path = path"/examples/font-awesome.html"

override def createUI(): Future[Unit] = for {
_ <- FontAwesome.load()
} yield {
val iconView = new FontAwesomeView {
value := FontAwesome.Brands.Android
font.weight := FontWeight("bold")
font.size := 128.0
color := Color.Blue
position.center := container.size.center
position.middle := container.size.middle
}
container.children += iconView
}
}
@@ -0,0 +1,27 @@
package io.youi.font

import io.youi.component.extras.HTMLComponent
import io.youi.dom._
import io.youi.theme.{FontAwesomeViewTheme, Theme}
import org.scalajs.dom._

class FontAwesomeView(protected val element: html.Element,
val existing: Boolean = false) extends HTMLComponent[html.Element] with FontAwesomeViewTheme {
def this() = {
this(create[html.Element]("i"))
}

FontAwesome.load()

override protected def defaultParentTheme: Theme = FontAwesomeView

override def componentType: String = "FontAwesomeView"
}

object FontAwesomeView extends FontAwesomeViewTheme {
override protected def defaultParentTheme: Theme = HTMLComponent

def existing(id: String, in: html.Element = document.body): FontAwesomeViewTheme = {
new FontAwesomeView(in.byId[html.Element](id), existing = true)
}
}
@@ -0,0 +1,28 @@
package io.youi.theme

import io.youi.font.{FontAwesome, FontAwesomeIcon}
import io.youi.theme.StyleConnect.withElement
import io.youi.theme.mixins.HTMLFontTheme

trait FontAwesomeViewTheme extends HTMLComponentTheme with HTMLFontTheme {
lazy val value: StyleProp[FontAwesomeIcon] = style[FontAwesomeIcon]("value", FontAwesome.None, Some(new StyleConnect[FontAwesomeIcon] {
override def init(theme: Theme, v: StyleProp[FontAwesomeIcon], name: String): Unit = {
var previous = Option.empty[FontAwesomeIcon]
v.attachAndFire { icon =>
withElement(theme) { e =>
previous.foreach { i =>
e.classList.remove(i.prefix)
e.classList.remove(s"fa-${i.name}")
}
if (icon != FontAwesome.None) {
e.classList.add(icon.prefix)
e.classList.add(s"fa-${icon.name}")
previous = Some(icon)
} else {
previous = None
}
}
}
}
}), updatesTransform = false)
}
@@ -1,7 +1,7 @@
package io

import io.youi.event.KeyEvent
import io.youi.font.{GoogleFont, GoogleFontWeight, MaterialIcon, MaterialIconView}
import io.youi.font.{FontAwesome, GoogleFont, GoogleFontWeight, MaterialIcon, MaterialIconView}
import io.youi.paint.Paint
import io.youi.task.PartialAnimate
import io.youi.theme.StyleProp
@@ -14,6 +14,7 @@ import typekit.{GoogleConfig, WebFont, WebFontConfiguration}
import scala.concurrent.{Future, Promise}
import scala.language.implicitConversions
import scala.scalajs.js
import scribe.Execution.global

package object youi {
lazy val ppi: Double = {
@@ -37,6 +38,14 @@ package object youi {
}
}

implicit class ExtendedFontAwesome(font: FontAwesome) {
def load(): Future[FontAwesome] = {
dom.addScript("https://kit.fontawesome.com/afbab8b8a9.js", addToHead = true).map { _ =>
font
}
}
}

implicit class ExtendedGoogleFont(font: GoogleFont) {
def load(): Future[GoogleFont] = {
val promise = Promise[GoogleFont]

0 comments on commit 7c893c5

Please sign in to comment.
You can’t perform that action at this time.