Fusion icon indicating copy to clipboard operation
Fusion copied to clipboard

Add attributes support to New

Open dphfox opened this issue 4 years ago • 5 comments

Currently it's not possible to set attributes on instances being created with the New function. Additionally, it's not possible to listen for changes on attributes.

While attributes aren't particularly useful within Fusion, having support for them would make it easier to integrate Fusion with legacy UI codebases which depend on them.

dphfox avatar Jun 23 '21 17:06 dphfox

Possible API (implemented as part of the New function):

New "Instance" {
    [Attribute "Foo"] = "bar", -- if a state or computed object, binds to the attribute
    [AttributeChange "Foo"] = function(newValue)
        print("The attribute was changed to:", newValue)
    end
}

dphfox avatar Jun 23 '21 20:06 dphfox

An alternate possible API from Twitter (adapted from a suggestion by @/bradsharppp):

New "Instance" {
    [Attributes] = {
        Foo = "bar",
        [OnChange "Foo"] = function(newValue)
            print("The attribute was changed to:", newValue)
        end
    }
}

Alternatively, we could keep AttributeChange separate from the Attributes table?

dphfox avatar Jun 23 '21 20:06 dphfox

I personally prefer the first option as it is very similar to the API for normal properties.

blake-mealey avatar Jun 24 '21 14:06 blake-mealey

I personally prefer the first option as it is very similar to the API for normal properties.

Agreed - I don't personally see many use cases where a lot of attributes are being used at once within the context of Fusion, so I think it'd overall be cleaner not to use a nested table.

dphfox avatar Jun 24 '21 17:06 dphfox

I personally prefer the first option as it is very similar to the API for normal properties.

Agreed - I don't personally see many use cases where a lot of attributes are being used at once within the context of Fusion, so I think it'd overall be cleaner not to use a nested table.

Same here! I think fusion should eliminate the times where you normally in Roact get huge files with lots of scopes.

Gargafield avatar Sep 15 '21 07:09 Gargafield