-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor template, break one big template into many smaller templates (…
- Loading branch information
Showing
9 changed files
with
144 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"scalawind": patch | ||
--- | ||
|
||
refactor template, break the one big template into many smaller templates |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{{#if laminar}} | ||
import com.raquo.laminar.api.L.cls | ||
|
||
object clx: | ||
def apply(styles: String) = cls(styles) | ||
def :=(styles: String) = cls(styles) | ||
{{/if}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{{#if scalajsReact}} | ||
import japgolly.scalajs.react.vdom.html_<^.* | ||
|
||
object clx: | ||
def apply(styles: String): TagMod = ^.cls := styles | ||
def :=(styles: String): TagMod = ^.cls := styles | ||
{{/if}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
implicit inline def sw(inline tailwind: Tailwind): String = | ||
${ swImpl('tailwind) } | ||
|
||
def methodNameToTailwindClass(rawName: String) = | ||
val name = if rawName.startsWith("_") && rawName.charAt(1).isDigit then rawName.stripPrefix("_") else rawName | ||
name.replace("_", "-").replace("per", "/").replace("dot", ".") | ||
|
||
def swImpl(tailwindExpr: Expr[Tailwind])(using Quotes): Expr[String] = | ||
import quotes.reflect.* | ||
|
||
def extractClassNames(term: Term, prefix: String = "", important: Boolean = false): List[String] = | ||
var stack = List((term, prefix, important)) | ||
var classNames = List.empty[String] | ||
|
||
while | ||
stack.nonEmpty | ||
do | ||
val (currentTerm, currentPrefix, currentImportant) = stack.head | ||
stack = stack.tail | ||
|
||
currentTerm match | ||
case Apply(Select(inner, "important"), List(styles)) => | ||
stack = (styles, currentPrefix, true) :: stack | ||
stack = (inner, currentPrefix, currentImportant) :: stack | ||
case Inlined(_, _, inner) => | ||
stack = (inner, currentPrefix, currentImportant) :: stack | ||
case Select(inner, name) => | ||
val methodName = methodNameToTailwindClass(name) | ||
val className = s"$currentPrefix${if (currentImportant) "!" else ""}${methodName}" | ||
classNames = classNames :+ className | ||
stack = (inner, currentPrefix, currentImportant) :: stack | ||
case Ident("tw") => | ||
// No action needed, just continue processing the remaining stack | ||
case Apply(Ident(name), List(arg)) => | ||
val methodName = methodNameToTailwindClass(name) | ||
val className = s"$currentPrefix${if (currentImportant) "!" else ""}${methodName}" | ||
classNames = classNames :+ className | ||
stack = (arg, currentPrefix, currentImportant) :: stack | ||
case Apply(Select(inner, name), List(Literal(StringConstant(value)))) if name == "raw" => | ||
val className = s"$currentPrefix${if (currentImportant) "!" else ""}$value" | ||
classNames = classNames :+ className | ||
stack = (inner, currentPrefix, currentImportant) :: stack | ||
case Apply(Select(inner, name), List(Literal(StringConstant(opacity)))) if name.endsWith("$") => | ||
val methodName = methodNameToTailwindClass(name.stripSuffix("$")) | ||
val className = s"$currentPrefix${if (currentImportant) "!" else ""}${methodName}/${opacity}" | ||
classNames = classNames :+ className | ||
stack = (inner, currentPrefix, currentImportant) :: stack | ||
case Apply(Select(inner, name), List(Literal(StringConstant(value)))) => | ||
val methodName = methodNameToTailwindClass(name) | ||
val className = s"$currentPrefix${if (currentImportant) "!" else ""}${methodName}[$value]" | ||
classNames = classNames :+ className | ||
stack = (inner, currentPrefix, currentImportant) :: stack | ||
case Apply(Apply(Ident(name), args), List(Literal(StringConstant(value)))) => | ||
val methodName = methodNameToTailwindClass(name) | ||
val className = s"$currentPrefix${if (currentImportant) "!" else ""}${methodName}[$value]" | ||
classNames = classNames :+ className | ||
stack = args.map(arg => (arg, currentPrefix, currentImportant)) ++ stack | ||
case Apply(Select(Ident("tw"), name), List(inner)) => | ||
val methodName = methodNameToTailwindClass(name) | ||
stack = (inner, s"$currentPrefix${methodName}:", currentImportant) :: stack | ||
case Apply(Select(inner, "variant"), List(Literal(StringConstant(selector)), styles)) => | ||
val variantPrefix = s"$currentPrefix[$selector]:" // Use the selector as provided | ||
val styleClasses = extractClassNames(styles, variantPrefix, currentImportant) // Extract classes with the variant prefix | ||
classNames = classNames ++ styleClasses | ||
stack = (inner, currentPrefix, currentImportant) :: stack | ||
case Apply(Select(inner, name), args) => | ||
val methodName = methodNameToTailwindClass(name) | ||
val innerClasses = args.flatMap(arg => extractClassNames(arg, s"$currentPrefix${methodName}:")) | ||
classNames = classNames ++ innerClasses | ||
stack = (inner, currentPrefix, currentImportant) :: stack | ||
case unexpectedTerm => | ||
report.errorAndAbort(s"Unexpected term: $unexpectedTerm") | ||
|
||
classNames | ||
end extractClassNames | ||
|
||
val term = tailwindExpr.asTerm | ||
val classNames = extractClassNames(term) | ||
val combinedClasses = classNames.reverse.mkString(" ") | ||
{{#if previewCompliedResult}} | ||
report.info(s"$combinedClasses") | ||
{{/if}} | ||
Expr(combinedClasses) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
val tw = Tailwind() | ||
|
||
case class Tailwind(): | ||
{{#each modifiers}} | ||
def {{this.name}}(@unused styles: Tailwind): Tailwind = this | ||
{{/each}} | ||
{{#each opacityColors}} | ||
def {{this}}(@unused value: String): Tailwind = this | ||
{{/each}} | ||
{{#each standard}} | ||
{{this.doc}} | ||
def {{this.prop}}: Tailwind = this | ||
{{/each}} | ||
{{#each arbitrary}} | ||
def {{this.methodName}}(value: String): Tailwind = this | ||
{{/each}} | ||
def important(@unused styles: Tailwind): Tailwind = this | ||
def raw(@unused classString: String): Tailwind = this | ||
def variant(selector: String, styles: Tailwind): Tailwind = this |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters