Giter VIP home page Giter VIP logo

Comments (11)

renatoi avatar renatoi commented on July 26, 2024

I think this is a great idea. I'm not really sure how useful it is to allow users to set custom rules though.

Thoughts @src-code ?

from atomizer.

src-code avatar src-code commented on July 26, 2024

I think there's value in having developers create custom rulesets (eg, for helpers), but I don't expect developers to dig into our rule file and make edits all that often. So I think there could be value in doing this, though I'm not sure what the priority should be. Let's mark this for our 2.1 milestone for now.

from atomizer.

3den avatar 3den commented on July 26, 2024

@src-code I don't think people would want to add custom rules on their apps directly but I see that as Atomic gets more popular this kind of feature can be used by 3rd party to create atomic extensions.

For now the main goal of this suggestion is to remove code duplication from rules.js and make it easier to maintain.

from atomizer.

3den avatar 3den commented on July 26, 2024

also this would allow to have the rules split in different files, e.g:

// rules/index.js
Atomizer.setRules(require('./padding.js'));
Atomizer.setRules(require('./margin.js'));
...
Atomizer.setRules(require('./colors.js'));
...

module.exports = Atomizer.getRules();

from atomizer.

3den avatar 3den commented on July 26, 2024

In the end what Atomizer.getRules(); will return will be the same and no other file would need to be changed.

from atomizer.

3den avatar 3den commented on July 26, 2024

I am not thinking about people editing our rules directly instead they could create a node_module that simply add new rules to atomic. If someone wants those extra rules they just have to install that module.

from atomizer.

3den avatar 3den commented on July 26, 2024

This suggestion is related to #90

from atomizer.

src-code avatar src-code commented on July 26, 2024

Yeah, I like the idea of this one, we'll just want to think through the implications of this change (eg, in which cases would not want to inherit settings from the parent?) We definitely need a way in 2.0 to allow developers to add in multiple rulesets. I'm intrigued by the pattern you've suggested here (of calling Atomzier APIs from within each rule file) but suspect we may want the rule files to be less dependent on Atomizer, especially since getRules() would seemingly return the aggregate ruleset, so you'd have a harder time just loading a single ruleset for introspection...

from atomizer.

3den avatar 3den commented on July 26, 2024

@src-code that can be simple, bellow there is a example of how we can implement this:

// Atomizer.js

module.exports = {
    _map: {},
    _list: [],

    // the argument is a simple array
    setRules: function (rules) {
        // Sets all given rules
        rules.forEach(function (rule) {
            this.setRule(rule);
        }, this);
    },

    setRule: function (rule) {
        // Inherit from the parent object or from a new object
        var parent = this._map[rule.parent] || {};
        var newRule = Object.create(parent);   

        // Overrides from the parent properties
        Object.keys(rule).forEach(function (key) {     
              newRule[key] = rule[key];              
        }, this);

        // Pushes the newRule to _map and to the _list
        // Some logic can be added to make sure the same rule is not added twice 
        // or to replace an existent rule
        this._map[rule.id] = newRule;
        this._list.push(newRule);
    },

    getRules: function () {
        // returns all rules as a list this output is an array of rules, the same as what we currently have.
        return this._list;
    }
};

The object above allows us to easy add new rules, we can add rules provided from separated files or from configs we can add some settings to allow certain rules to be overwritten or to prevent that to happen... It is very flexible and easy to maintain/improve.

from atomizer.

3den avatar 3den commented on July 26, 2024

the rules.js can be very simple and clean too:

// rules/index.js
[
   'padding',
   'margin',
   'position',
   ...
].forEach(function (file) {
    Atomizer.setRules(require('./' + file));
}); 

// This will return exactly the same as the current `rules.js`.
module.exports = Atomizer.getRules();

Each rules definition file exports just a simple array similar to what we have but he can use inheritance:

// rules/padding.js

module.exports = [
 {
        type: 'pattern',
        id: 'padding',
        name: 'Padding (all edges)',
        prefix: '.P-',
        properties: ['padding'],
        allowCustom: true,
        allowSuffixToValue: true,
        rules: [
            {suffix: '0', values: [0]},
            {suffix: 'inh', values: ['inherit']}
        ]
    },
    // top
    {
        parent: 'padding',
        id: 'padding-top',
        name: 'Padding top',
        prefix: '.Pt-',
        properties: ['padding-top'],
    },
    // end
    {
        parent: 'padding',
        id: 'padding-end',
        name: 'Padding end',
        prefix: '.Pend-',
        properties: ['padding-$END'],
    },
    ...
];

As you can see this architecture will make much easier to create new rule files or to load rules from configs. By reading rules from a config we allow custom helpers, plugins and a lot of extensions built by the community ;)

from atomizer.

3den avatar 3den commented on July 26, 2024

About inheritance, on the exemples above it is optional. If you dont want to use the parent you dont have to, in that case you just need to define all props of that rule, just like we currently do.

The performance and memory consuption is better too because by using object inheritance we are not copying duplicated props, each object only defines what it has diferent from its parent.

from atomizer.

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.