Code Monkey home page Code Monkey logo

classy-yaml's Introduction

Classy::Yaml

This was created to provide convenient utility class grouping for environments without a bundler (or any situation where you might not want to add custom css classes). YAML files are a perfect structure for these situations.

Usage

After installing the gem, the helper method yass will be available from anywhere. It looks for the definitions in the YAML file config/utility_classes.yml (note: you must add this yourself).

The YAML structure should follow this format:

btn:
  blue: "text-blue-200 bg-blue-500"
  yellow: "text-yellow-200 bg-blue-500"

Then, you can add these classes to any element by calling yass(btn: :blue) or yass(btn: :yellow).

You may wonder though, what about shared classes you want all nested definitions to inherit? There is a special syntax for this type of inheritance:

btn:
  base: "px-3 py-2"
  blue: "text-blue-200 bg-blue-500"
  yellow: "text-yellow-200 bg-blue-500"

Now, calling yass(btn: :blue) or yass(btn: :yellow) will ALSO pull in the classes from btn: :base.

Optionally Skipping Base

You can optionally skip including the base on a yass call by including the key/value skip_base: true. So, using the example above, we can perform:

btn:
  base: "px-3 py-2"
  blue: "text-blue-200 bg-blue-500"
  yellow: "text-yellow-200 bg-blue-500"

Now, calling yass(btn: :blue, skip_base: true) and this will skip pulling in the classes from btn: :base. This is helpful when defining animation classes and you only want to include the different classes, such as active and inactive for instance.

ViewComponent

There is a special helper built for ViewComponent and sidecar assets. In your example_component.rb, add the line include Classy::Yaml::ComponentHelpers. This helper will tell yass to check if there is a example_component.yml file, and first use that for definitions. If the definitions aren't found in the example_component.yml, then it will fallback to config/utility_classes.yml.

So, it may look like this

# config/utility_classes.yml
btn:
  base: "px-3 py-2"
  blue: "text-blue-200 bg-blue-500"
  yellow: "text-yellow-200 bg-blue-500"
  
  
# app/components/example_component/example_component.yml
btn:
  purple: "text-purple-200 bg-blue-500"
  

# app/components/example_component/example_component.html.erb

<button class="<%=yass(btn: :purple)%>">Classy Button</button>

# will add the classes "px-3 py-2 text-purple-200 bg-blue-500"

As you can see, this will merge definitions found in the ExampleComponent's sidecar asset with the main application's definitions. Not only will it merge, but it will overwrite them as well, allowing for true customization on a per-component basis:

# config/utility_classes.yml
btn:
  base: "px-3 py-2"
  blue: "text-blue-200 bg-blue-500"
  yellow: "text-yellow-200 bg-blue-500"
  
  
# app/components/example_component/example_component.yml
btn:
  blue: "text-blue-50"
  

# app/components/example_component/example_component.html.erb

<button class="<%=yass(btn: :purple)%>">Classy Button</button>

# will add the classes "px-3 py-2 text-blue-50"

Configuration

You can configure the gem by creating an initializer in your Rails app. The following options are available:

Classy::Yaml.setup do |config|
  # The default path for the utility classes YAML file 
  config.default_path = "config/utility_classes.yml"
  
  # Any extra files you want to load in addition to the default file. The last file loaded will overwrite any previous definitions
  config.extra_files = [
    "app/yamls/extra_styles.yml"
  ]
end

Installation

Add this line to your application's Gemfile:

gem 'classy-yaml'

And then execute:

$ bundle

Or install it yourself as:

$ gem install classy-yaml

If using purging for styles, be sure to add the YAML files to the purge directories. For example, below is a tailwind.config.js:

purge: [
   "./config/utility_classes.yml",
   "./app/components/**/*.yml" // If using view component sidecar assets
]

Contributing

This is my first attempt at an actual gem usable for all. Any and all suggestions are absolutely welcome. I'm not the cleanest programmer, so code quality suggestions are welcome too 👍 I extracted this logic from my private work and I felt it was useful enough to be worth publishing.

License

The gem is available as open source under the terms of the MIT License.

classy-yaml's People

Contributors

feipinghuang avatar joemasilotti avatar tonksthebear avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

classy-yaml's Issues

Require minimum amount of gems possible

Hey there! Congrats on your first gem! I stumbled across this gem on Twitter and thought I’d provide you a little feedback.

Having rails as a dependency is not great for a gem. Generally, it’s suggested to only include the specific parts of Rails that you actually use.

Just skimming your codebase, it seems like you only need activesupport, actionpack, and railties.

By adding rails as a whole as a dependency, you’re requiring apps to include ActiveRecord, ActionMailer, ActionMailbox, ActiveJob, ActionCable, etc as a dependency. This isn’t ideal because some apps try to cut down on their dependencies by avoiding parts of Rails that they don’t use.

Here’s an example of a Basecamp gem that extends the rails query logs to include info about the current action. Instead of requiring rails, it requires actionpack and activerecord. https://github.com/basecamp/marginalia/blob/master/marginalia.gemspec

How do I run the tests?

$ ./bin/test produces

/Users/joemasilotti/.rbenv/versions/2.7.1/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:92:in `require': cannot load such file -- rails/plugin/test (LoadError)

$ rake produces

/Users/joemasilotti/workspace/forks/classy-yaml/test/dummy/config/application.rb:7:in `<top (required)>': uninitialized constant Bundler (NameError)

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.