diff --git a/web/svg/src/jsMain/kotlin/org/jetbrains/compose/web/svg/svg.kt b/web/svg/src/jsMain/kotlin/org/jetbrains/compose/web/svg/svg.kt index 53cfc97f34..ff550ee82f 100644 --- a/web/svg/src/jsMain/kotlin/org/jetbrains/compose/web/svg/svg.kt +++ b/web/svg/src/jsMain/kotlin/org/jetbrains/compose/web/svg/svg.kt @@ -68,7 +68,7 @@ fun Svg( TagElement( elementBuilder = Svg, applyAttrs = { - viewBox?.let { attr("viewBox", it) } + viewBox?.let { viewBox(it) } attrs?.invoke(this) }, content = content @@ -85,7 +85,7 @@ fun ElementScope.SvgA( TagElement( elementBuilder = A, applyAttrs = { - attr("href", href) + href(href) attrs?.invoke(this) }, content = content @@ -104,9 +104,9 @@ fun ElementScope.Circle( TagElement( elementBuilder = Circle, applyAttrs = { - attr("cx", cx.toString()) - attr("cy", cy.toString()) - attr("r", r.toString()) + cx(cx) + cy(cy) + r(r) attrs?.invoke(this) }, content = content @@ -126,9 +126,9 @@ fun ElementScope.Circle( TagElement( elementBuilder = Circle, applyAttrs = { - attr("cx", cx.toString()) - attr("cy", cy.toString()) - attr("r", r.toString()) + cx(cx) + cy(cy) + r(r) attrs?.invoke(this) }, content = content @@ -147,8 +147,8 @@ fun ElementScope.SvgText( TagElement( elementBuilder = Text, applyAttrs = { - attr("x", x.toString()) - attr("y", y.toString()) + x(x) + y(y) attrs?.invoke(this) }, content = { @@ -167,8 +167,8 @@ fun ElementScope.View( TagElement( elementBuilder = View, applyAttrs = { - attr("id", id) - attr("viewBox", viewBox) + id(id) + viewBox(viewBox) attrs?.invoke(this) }, content = null @@ -188,10 +188,10 @@ fun ElementScope.Rect( TagElement( elementBuilder = Rect, applyAttrs = { - attr("x", x.toString()) - attr("y", y.toString()) - attr("width", width.toString()) - attr("height", height.toString()) + x(x) + y(y) + width(width) + height(height) attrs?.invoke(this) }, content = content @@ -211,10 +211,35 @@ fun ElementScope.Rect( TagElement( elementBuilder = Rect, applyAttrs = { - attr("x", x.toString()) - attr("y", y.toString()) - attr("width", width.toString()) - attr("height", height.toString()) + x(x) + y(y) + width(width) + height(height) + attrs?.invoke(this) + }, + content = content + ) +} + +@Composable +@ExperimentalComposeWebSvgApi +fun ElementScope.Rect( + width: Number, + height: Number, + rx: Number, + ry: Number = rx, + transform: String, + attrs: AttrBuilderContext? = null, + content: ContentBuilder? = null +) { + TagElement( + elementBuilder = Rect, + applyAttrs = { + width(width) + height(height) + rx(rx) + ry(ry) + transform(transform) attrs?.invoke(this) }, content = content @@ -234,10 +259,10 @@ fun ElementScope.Ellipse( TagElement( elementBuilder = Ellipse, applyAttrs = { - attr("cx", cx.toString()) - attr("cy", cy.toString()) - attr("rx", rx.toString()) - attr("ry", ry.toString()) + cx(cx) + cy(cy) + rx(rx) + ry(ry) attrs?.invoke(this) }, content = content @@ -257,10 +282,10 @@ fun ElementScope.Ellipse( TagElement( elementBuilder = Ellipse, applyAttrs = { - attr("cx", cx.toString()) - attr("cy", cy.toString()) - attr("rx", rx.toString()) - attr("ry", ry.toString()) + cx(cx) + cy(cy) + rx(rx) + ry(ry) attrs?.invoke(this) }, content = content @@ -278,7 +303,7 @@ fun ElementScope.Symbol( TagElement( elementBuilder = Symbol, applyAttrs = { - id?.let { attr("id", it) } + id?.let { id(it) } attrs?.invoke(this) }, content = content @@ -295,7 +320,7 @@ fun ElementScope.Use( TagElement( elementBuilder = Use, applyAttrs = { - attr("href", href) + href(href) attrs?.invoke(this) }, content = content @@ -315,10 +340,10 @@ fun ElementScope.Line( TagElement( elementBuilder = Line, applyAttrs = { - attr("x1", x1.toString()) - attr("y1", y1.toString()) - attr("x2", x2.toString()) - attr("y2", y2.toString()) + x1(x1) + y1(y1) + x2(x2) + y2(y2) attrs?.invoke(this) }, content = content @@ -338,10 +363,10 @@ fun ElementScope.Line( TagElement( elementBuilder = Line, applyAttrs = { - attr("x1", x1.toString()) - attr("y1", y1.toString()) - attr("x2", x2.toString()) - attr("y2", y2.toString()) + x1(x1) + y1(y1) + x2(x2) + y2(y2) attrs?.invoke(this) }, content = content @@ -359,7 +384,7 @@ fun ElementScope.ClipPath( TagElement( elementBuilder = ClipPath, applyAttrs = { - attr("id", id) + id(id) attrs?.invoke(this) }, content = content @@ -376,7 +401,7 @@ fun ElementScope.Path( TagElement( elementBuilder = Path, applyAttrs = { - attr("d", d) + d(d) attrs?.invoke(this) }, content = content @@ -406,7 +431,7 @@ fun ElementScope.Image( TagElement( elementBuilder = Image, applyAttrs = { - attr("href", href) + href(href) attrs?.invoke(this) }, content = content @@ -423,7 +448,7 @@ fun ElementScope.Mask( TagElement( elementBuilder = Mask, applyAttrs = { - id?.let { attr("id", it) } + id?.let { id(it) } attrs?.invoke(this) }, content = content @@ -453,7 +478,7 @@ fun ElementScope.Pattern( TagElement( elementBuilder = Pattern, applyAttrs = { - attr("id", id) + id(id) attrs?.invoke(this) }, content = content @@ -470,7 +495,7 @@ fun ElementScope.Polygon( TagElement( elementBuilder = Polygon, applyAttrs = { - attr("points", points.toList().chunked(2).joinToString(" ") { it.joinToString(",") }) + points(points) attrs?.invoke(this) }, content = content @@ -487,7 +512,7 @@ fun ElementScope.Polyline( TagElement( elementBuilder = Polyline, applyAttrs = { - attr("points", points.toList().chunked(2).joinToString(" ") { it.joinToString(",") }) + points(points) attrs?.invoke(this) }, content = content @@ -504,7 +529,7 @@ fun ElementScope.TextPath( TagElement( elementBuilder = TextPath, applyAttrs = { - attr("href", href) + href(href) attrs?.invoke(this) }, content = { @@ -562,7 +587,7 @@ fun ElementScope.LinearGradient( TagElement( elementBuilder = LinearGradient, applyAttrs = { - id?.let { attr("id", it) } + id?.let { id(it) } attrs?.invoke(this) }, content = content @@ -580,7 +605,7 @@ fun ElementScope.RadialGradient( TagElement( elementBuilder = RadialGradient, applyAttrs = { - id?.let { attr("id", it) } + id?.let { id(it) } attrs?.invoke(this) }, content = content @@ -706,8 +731,8 @@ fun ElementScope.Set( TagElement( elementBuilder = Set, applyAttrs = { - attr("attributeName", attributeName) - attr("to", to) + attributeName(attributeName) + to(to) attrs?.invoke(this) }, content = content diff --git a/web/svg/src/jsMain/kotlin/org/jetbrains/compose/web/svg/svgAttrs.kt b/web/svg/src/jsMain/kotlin/org/jetbrains/compose/web/svg/svgAttrs.kt new file mode 100644 index 0000000000..4d0d74ac57 --- /dev/null +++ b/web/svg/src/jsMain/kotlin/org/jetbrains/compose/web/svg/svgAttrs.kt @@ -0,0 +1,158 @@ +/* + * Copyright 2020-2022 JetBrains s.r.o. and respective authors and developers. + * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE.txt file. + */ + +package org.jetbrains.compose.web.svg + +import org.jetbrains.compose.web.attributes.* +import org.jetbrains.compose.web.css.* +import org.w3c.dom.svg.* + +fun AttrsScope.width(px: Number) { + attr("width", px.toString()) +} + +fun AttrsScope.height(px: Number) { + attr("height", px.toString()) +} + +fun AttrsScope.width(value: CSSLengthOrPercentageValue) { + attr("width", value.toString()) +} + +fun AttrsScope.height(value: CSSLengthOrPercentageValue) { + attr("height", value.toString()) +} + +fun AttrsScope.xmlns(nameSpace: String) { + attr("xmlns", nameSpace) +} + +fun AttrsScope.attributeName(attributeName: String) { + attr("attributeName", attributeName) +} + +fun AttrsScope.fill(color: String) { + attr("fill", color) +} + +fun AttrsScope.fillRule(fill: String) { + attr("fill-rule", fill) +} + +fun AttrsScope.href(href: String) { + attr("href", href) +} + +fun AttrsScope.viewBox(viewBox: String) { + attr("viewBox", viewBox) +} + +fun AttrsScope.transform(transform: String) { + attr("transform", transform) +} + +fun AttrsScope.d(d: String) { + attr("d", d) +} + +fun AttrsScope.points(points: String) { + attr("points", points) +} + +fun AttrsScope.points(vararg points: Number) { + attr("points", points.toList().chunked(2).joinToString(" ") { it.joinToString(",") }) +} + +fun AttrsScope.cx(cx: Number) { + attr("cx", cx.toString()) +} + +fun AttrsScope.cy(cy: Number) { + attr("cy", cy.toString()) +} + +fun AttrsScope.r(r: Number) { + attr("r", r.toString()) +} + +fun AttrsScope.rx(rx: Number) { + attr("rx", rx.toString()) +} + +fun AttrsScope.ry(ry: Number) { + attr("ry", ry.toString()) +} + +fun AttrsScope.x(x: Number) { + attr("x", x.toString()) +} + +fun AttrsScope.y(y: Number) { + attr("y", y.toString()) +} + +fun AttrsScope.x1(x1: Number) { + attr("x1", x1.toString()) +} + +fun AttrsScope.y1(y1: Number) { + attr("y1", y1.toString()) +} + +fun AttrsScope.x2(x2: Number) { + attr("x2", x2.toString()) +} + +fun AttrsScope.y2(y2: Number) { + attr("y2", y2.toString()) +} + +fun AttrsScope.cx(cx: CSSLengthOrPercentageValue) { + attr("cx", cx.toString()) +} + +fun AttrsScope.cy(cy: CSSLengthOrPercentageValue) { + attr("cy", cy.toString()) +} + +fun AttrsScope.r(r: CSSLengthOrPercentageValue) { + attr("r", r.toString()) +} + +fun AttrsScope.rx(rx: CSSLengthOrPercentageValue) { + attr("rx", rx.toString()) +} + +fun AttrsScope.ry(ry: CSSLengthOrPercentageValue) { + attr("ry", ry.toString()) +} + +fun AttrsScope.x(x: CSSLengthOrPercentageValue) { + attr("x", x.toString()) +} + +fun AttrsScope.y(y: CSSLengthOrPercentageValue) { + attr("y", y.toString()) +} + +fun AttrsScope.x1(x1: CSSLengthOrPercentageValue) { + attr("x1", x1.toString()) +} + +fun AttrsScope.y1(y1: CSSLengthOrPercentageValue) { + attr("y1", y1.toString()) +} + +fun AttrsScope.x2(x2: CSSLengthOrPercentageValue) { + attr("x2", x2.toString()) +} + +fun AttrsScope.y2(y2: CSSLengthOrPercentageValue) { + attr("y2", y2.toString()) +} + +fun AttrsScope.to(to: String) { + attr("to", to) +}