Code Monkey home page Code Monkey logo

Comments (2)

MangelMaxime avatar MangelMaxime commented on July 19, 2024 2

I want to provide something equivalent to ReactLeaflet bindings.

Remove the IProp thing completly, have a clean separation between html and svg elements.

Fable.Helpers.React:

// Contains the generic React helpers
namespace Fable.Helpers.React

[<AutoOpen>]
module Common =

    [<Import("createElement", from="react")>]
    let createElement(comp: obj, props: obj, [<ParamList>] children: obj) =
        HTMLNode.Empty :> ReactElement

    let inline ofFunction<'P> (f: 'P -> ReactElement) (props: 'P) (children: ReactElement seq): ReactElement =

    // Example
    /// Alias of `ofString`
    let inline str (s: string): ReactElement = unbox s

    /// Cast a string to a React element (erased in runtime)
    let inline ofString (s: string): ReactElement = unbox s

    /// Cast an option value to a React element (erased in runtime)
    let inline ofOption (o: ReactElement option): ReactElement =
        match o with Some o -> o | None -> null // Option.toObj(o)

    /// OBSOLETE: Use `ofOption`
    [<System.Obsolete("Use ofOption")>]
    let inline opt (o: ReactElement option): ReactElement = ofOption o

    /// Cast an int to a React element (erased in runtime)
    let inline ofInt (i: int): ReactElement = unbox i

    /// Cast a float to a React element (erased in runtime)
    let inline ofFloat (f: float): ReactElement = unbox f

    /// Returns a list **from .render() method**
    let inline ofList (els: ReactElement list): ReactElement = unbox(List.toArray els)

    /// Returns an array **from .render() method**
    let inline ofArray (els: ReactElement array): ReactElement = unbox els

Fable.Helpers.React.Html

module  Fable.Helpers.React.Html

type CSSProp =
    | AlignContent of obj
    | AlignItems of obj
    | AlignSelf of obj
    | Custom of string * obj

type Props =
    // Concatenation of HtmlProps & HtmlAttr
    | Key of string
    | Ref of (React.Element -> unit)
    | Custom of string * obj
    | Style of CSSProp list

// All the Html element helpers
let inline a b c = domEl "a" b c
let inline abbr b c = domEl "abbr" b c
let inline address b c = domEl "address" b c
let inline article b c = domEl "article" b c
let inline aside b c = domEl "aside" b c
let inline audio b c = domEl "audio" b c
module Fable.Helpers.React.Svg

type Props =
    // Concatenation of HtmlProps & HtmlAttr
    | Key of string
    | Ref of (React.Element -> unit)
    | Custom of string * obj
    | Style of CSSProp list

// All the Svg element helpers
let inline svg b c = svgEl "svg" b c
let inline circle b c = svgEl "circle" b c
let inline clipPath b c = svgEl "clipPath" b c
let inline defs b c = svgEl "defs" b c
let inline ellipse b c = svgEl "ellipse" b c

So we can have this kind of API:

// Old api
rect [ SVGAttr.X model.AxisYWidth
       SVGAttr.Y (offsetY - model.RowHeight)
       SVGAttr.Width barAreaWidth
       SVGAttr.Height model.RowHeight
       SVGAttr.Fill "#fff" ]
    [ ]

// New Api
Svg.rect [ Svg.X model.AxisYWidth
           Svg.Y (offsetY - model.RowHeight)
           Svg.Width barAreaWidth
           Svg.Height model.RowHeight
           Svg.Fill "#fff" ]
    [ ]

I guess my problem is that the current API isn't perfect and you can still write invalid code. And the IProp thing feel really like a hack for me. In general in bindings people do:

    type [<RequireQualifiedAccess>] Treemap =
        interface Fable.Helpers.React.Props.IProp

And that's wrong because you can't put Html props to a TreeMap for example. And I would like to introduce:

type CustomType =
    | [<Flatten>] HtmlProps of Html.Props list

Like that we mention that CustomType can accept Html.Props and only Html.Props not svg etc.

After writing this not sure if everyone would like to use this kind of API... And it's probably more a caprice from me.

Current API is probably good enough and next steps should probably be to implement a strongly typed DSL for CSS, units etc.

If we want to experiment it we probably should to try in a separate library. I will close this for now at least we have it for reference.

from fable-react.

alfonsogarciacaro avatar alfonsogarciacaro commented on July 19, 2024

You mean #73? I think at the end we decided the changes didn't bring enough benefit to justify the API change. But sure, a new major release is an opportunity to add some breaking changes, though we need to be careful not to force users to rewrite too much code. What do you have in mind?

from fable-react.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.