Skip to content

Commit 548b3a2

Browse files
zaaackalfonsogarciacaro
authored andcommitted
Improve React-style attributes in ssr
1 parent ef55dcb commit 548b3a2

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

Samples/SSRSample/src/Client/View.fs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,4 +183,19 @@ let view (model: Model) (dispatch) =
183183
ofFunction fnComp { text = "I'm rendered by Function Component!"} []
184184
ofFunction fnCompWithChildren { text = " I'm rendered by Function Component! "; children=[||]} [ span [] [ str " I'm rendered by children!"] ]
185185
]
186+
187+
div [ ClassName "test-case" ] [
188+
span [ ClassName "label" ] [ str "Test void elements:" ]
189+
hr [ Style [ BorderColor "green" ] ]
190+
br []
191+
]
192+
193+
div [ ClassName "test-case" ] [
194+
span [ ClassName "label" ] [ str "Test add slug to attributes:" ]
195+
div
196+
[ HTMLAttr.Custom ("contentEditable", "true")
197+
HTMLAttr.Custom ("placeholder", "I'm editable!")
198+
Style [ CSSProp.Custom ("WebkitTransform", "translateX(30px)"); CSSProp.Custom ("WebkitTransformOrigin", "0 0") ] ]
199+
[ ]
200+
]
186201
]

Samples/SSRSample/src/Client/index.css

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,18 @@ body {
4343
border: 1px solid #f93;
4444
}
4545

46-
.children-comp > span {
47-
margin-bottom: 5px;
46+
.children-comp > div {
47+
margin-bottom: 10px;
4848
border: 1px solid #0cb3f0;
4949
padding: 5px 10px;
5050
display: block;
5151
}
52+
53+
[placeholder][contenteditable]:empty:before {
54+
content: attr(placeholder);
55+
color: #9b9b9b;
56+
}
57+
58+
[placeholder][contenteditable]:empty:focus:before {
59+
content: "";
60+
}

src/Fable.React/Fable.Helpers.ReactServer.fs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ let private cssProp (key: string) (value: obj) =
4949

5050
key + ":" + value + ";"
5151

52-
let private cssPropRegex = Regex("([A-Z])")
52+
let private slugRegex = Regex("([A-Z])", RegexOptions.Compiled)
53+
let inline private slugKey key =
54+
slugRegex.Replace(string key, "-$1").ToLower()
5355

5456
let private renderCssProp (prop: CSSProp): string =
5557
match prop with
@@ -458,7 +460,7 @@ let private renderCssProp (prop: CSSProp): string =
458460
| WritingMode v -> cssProp "writing-mode" v
459461
| ZIndex v -> cssProp "z-index" v
460462
| Zoom v -> cssProp "zoom" v
461-
| CSSProp.Custom (key, value) -> cssProp key (cssPropRegex.Replace(string value, "-$1").ToLower())
463+
| CSSProp.Custom (key, value) -> cssProp (slugKey key) value
462464

463465
let inline boolAttr (key: string) (value: bool) = if value then key else ""
464466
let inline strAttr (key: string) (value: string) = key + "=\"" + (escapeHtml value) + "\""
@@ -616,7 +618,7 @@ let private renderHtmlAttr (attr: HTMLAttr): string =
616618
let css = css.[0..css.Length - 2]
617619
strAttr "style" css
618620

619-
| Custom (key, value) -> strAttr key (string value)
621+
| Custom (key, value) -> strAttr (key.ToLower()) (string value)
620622
| Data (key, value) -> strAttr ("data-" + key) (string value)
621623

622624
let private renderSVGAttr (attr: SVGAttr): string =
@@ -678,7 +680,7 @@ let private renderSVGAttr (attr: SVGAttr): string =
678680
| SVGAttr.Y1 v -> objAttr "y1" v
679681
| SVGAttr.Y2 v -> objAttr "y2" v
680682
| SVGAttr.Y v -> objAttr "y" v
681-
| SVGAttr.Custom (key, value) -> objAttr key value
683+
| SVGAttr.Custom (key, value) -> objAttr (slugKey key) value
682684

683685
let private renderAttrs (attrs: IProp seq) tag =
684686
let html = StringBuilder()

0 commit comments

Comments
 (0)