Giter Site home page Giter Site logo

angelmmiguel / material_icons Goto Github PK

View Code? Open in Web Editor NEW
291.0 12.0 62.0 2.9 MB

A simple Rails wrapper for Google Material Icons

License: MIT License

Ruby 61.46% HTML 24.01% JavaScript 2.99% CSS 11.55%
material-icons rails css rails-asset-pipeline gem rails-engine html css-icon

material_icons's Introduction

Gem Version Build Status

Material Icons for Rails

Material Icons for Rails

Material Design Icons is a +2500 set of icons based on Material Design guidelines. You can check all the icons in the official site.

This gem helps you to add this fantastic icon collection to your Rails projects easily. I keep the gem updated so you always will have the latest icons in your project.

Installation

To install the gem, add this line to your Gemfile:

gem 'material_icons'

Then, execute bundle install.

CSS

In your app/assets/stylesheets/application.css.erb file you need to reference material icons CSS.

Add this line at top of application.css:

/*
*= require material_icons
*/

NOTE: Depending on the comment style of application.css, you may need to add //= require material_icons instead of the previous line. See Rails Asset Pipeline for more info.

These files provide multiple CSS classes to use in your views. Main classes are:

.material-icons, .mi

Upgrade from v2.X to 4.X

The v4.X version changed how the icon shapes are set. Before, this gem used a method_missing approach (mi.face). Due to the amount of conflicting icon shape names, in v4.X I introduced the shape method to set the icon shape:

# In v2.X
mi.face

# In v4.X
mi.shape(:face)

When upgrading to v4.X, you will need to update all the calls to the mi and material_icon helpers.

Other styles

This gem supports the new Google Material Icons styles. They are imported as a different CSS stylesheet, so you can set only the icons that are required for your project. You can set just one style or several ones.

Every CSS stylesheet includes everything you need to use that style. If you don't plan to use the regular style, feel free to include any of the next styles and remove the *= require material_icons line from your application.css.erb file.

Outlined

/*
*= require material_icons.outlined
*/

Usage in your template:

<%= material_icon.shape(:face).outlined %>
# <i class="material-icons outlined">face</i> 

Sharp

/*
*= require material_icons.sharp
*/

Usage in your template:

<%= material_icon.shape(:face).sharp %>
# <i class="material-icons sharp">face</i> 

Round

/*
*= require material_icons.round
*/

Usage in your template:

<%= material_icon.shape(:face).round %>
# <i class="material-icons round">face</i> 

Two Tone

/*
*= require material_icons.twotone
*/

Usage in your template:

<%= material_icon.shape(:face).twotone %>
# <i class="material-icons twotone">face</i> 

Helper classes

This gem includes several helper sizes. To add them to your project, add the following stylesheet:

/*
*= require material_icons.helpers
*/

After adding it, the following helpers will be available:

/* Size */
.md-18
.md-24
.md-36
.md-48

/* Color */
.md-dark
.md-light

/* Rotation */
.r90
.r180
.r270
.flip-horizontal
.flip-vertical

Don't forget to see material_icons.css.erb ;).

Views

Google Material Icons uses a feature called ligatures. We can define the icon in the text of the HTML tag. Go to Google Guide to get more info.

An example of icon is:

<i class="material-icons">face</i>
<i class="material-icons md-36">face</i>

Ruby helpers

Material Icons provide two helpers to build the HTML code of icons. The methods are material_icon and mi. These helpers use cascade style to set the icon and options. Remember you first need to add the helpers stylesheet to use these helpers.

Using same example:

<%= material_icon.shape(:face) %>
# <i class="material-icons">face</i>

<%= material_icon.shape(:face).md_36 %>
# <i class="material-icons md-36">face</i>

# You can use string too
<%= material_icon.shape('3d_rotation').md_36 %>
# <i class="material-icons md-36">3d_rotation</i>

# Rotation and custom css class
<%= mi.shape(:face).r90.css_class('my_class') %>
# <i class="material-icons r90 my_class">face</i>

# Custom css and data attributes
<%= mi.shape(:face).css_class('my_class').html(data: {id: 1}) %>
# <i data-id="1" class="material-icons my_class">face</i>

Allowed methods are:

# Rotation methods
r90
r180
r270
flip_horizontal
flip_vertical

# Size methods
md_18
md_24
md_36
md_48

# Add some classes to the icon
css_class('classes')

# Add style to the icon
style('margin-top: 5px;')

# Add some HTML attributes to an icon. This method receive a Hash
html({ data: { id: 1 } })

Remember this is a helper, you always can use HTML syntax ;).

Compatibility

Ligature feature requires a supported browser:

Browser Version
Google Chrome >= 11
Mozilla Firefox >= 3.5
Apple Safari >= 5
Microsoft IE >= 10
Opera >= 15
Apple MobileSafari >= iOS 4.2
Android Browser >= 3.0

You can find the current browser support in can I use?.

Common issues

Slim templating engine

If you are using Slim templating engine in your Rails application, use double equal == method to render icons or the content won't be marked as HTML safe. We are working on this issue, because we use content_tag and it sets the text as HTML safe (see ActionView::Helpers::TagHelper line 146).

p.text
  == material_icon.shape(:search).md_18

Reference #9

Couldn't find file 'material_icons'

couldn't find file 'material_icons' with type 'application/javascript'

This error is related with the asset compilation of Rails. To fix it, just follow these steps:

Reference #10

Tested on

Tested with GitHub Actions.

Ruby versions:

  • 2.6
  • 2.7
  • 3.0
  • 3.1

Rails versions:

  • 6.0
  • 6.1
  • 7.0

They are using Material icons for Rails :)

  • Materialup.com: a big community to share, discover and learn about Material Design.
  • Easy Bills: a free tool to manage your finances.
  • Notebook.ai: your digital notebook is here. Notebook.ai grows with your ideas and collaborates back with you.

License

Google Material Icons are under Apache License v2.0. We'd love attribution in your app's about screen, but it's not required.

Material Icons gem is released under the MIT license. Copyright Angel M Miguel

material_icons's People

Contributors

angelmmiguel avatar janther avatar matthieua avatar raileanv avatar robothemes avatar rociiu avatar tricknotes avatar ykpythemind 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  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  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

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

material_icons's Issues

Inaccurate changelog

Released version of material_icons (2.2.1) does not include
Add to_str and to_ary methods to fix avoid errors with Array methods

Add to_str method

alias_method :to_str, :to_s in MaterialIcon Class
Rails safe_join calls to_str on objects passed before they are concatenated

Any plans on extending this to include more materialize components?

I'm actually looking to refactor my app's templates and noticed that the materialize CSS structure is causing a lot of repetition. So I was looking for some kind of Rails helper. This repo is the only thing I found so far, any plans on extending this to include other components? Could you elaborate a bit on how you think this could be structured? Would love to help!

Icons missing

@Angelmmiguel I know there was this pull request earlier to add missing icons but I'm still missing a lot. When I try to use the ones I find at the official site. For example key_off, psychology_alt, and task_alt are all missing. Also, I noticed some icons don't seem to match what shows on the official site. For example, the android icon shows the full android robot in implementation but on the official site, it just shows the android head. Am I looking in the wrong spot for icons? Or is the current font library not pulling in all the fonts? Any help is much apprecciated.

Calendar_today icon

The calendar_today icon looks broken as it renders with about 200px padding on the left side.

I wasn't able to fix it with CSS.

screenshot

View helper is not html safe

Hi,
found this thing recently:

rails (4.2.1)
material_icons (2.0.1)
bootstrap-material-design (0.2.2)

(note: only on production env)
RAILS_ENV=production

# views/.../_action.html.slim
button.btn[type="submit"]
  = material_icon.search.md_18

output was a link with raw inner html as text

<button class="btn" type="submit"><i class="material-icons md-18 ">search</i></button>

but with

# views/.../_action.html.slim
button.btn[type="submit"]
    = material_icon.search.md_18.to_s.html_safe

all worked fine.

Bug, working in Chrome

Hello, i had to add the next sentence in the css file for material icons work in Chrome browser (it only worked in mozilla)

.material-icons {
font-family: 'Material Icons';
font-weight: normal;
font-style: normal;
font-size: 24px; /* Preferred icon size */
display: inline-block;
line-height: 1;
text-transform: none;
letter-spacing: normal;
word-wrap: normal;
white-space: nowrap;
direction: ltr;

/* Support for all WebKit browsers. /
-webkit-font-smoothing: antialiased;
/
Support for Safari and Chrome. */
text-rendering: optimizeLegibility;

/* Support for Firefox. */
-moz-osx-font-smoothing: grayscale;

/* Support for IE. */
font-feature-settings: 'liga';
}

Stop setting icons through method_missing

As today, the way to set the icon in a specific material icons instance is to call a method:

mi.calendar

This approach is based on the special method_missing method from Ruby. At the beginning, I decided to use this approach as it simplifies the way you set the icon. However, I quickly noticed some drawbacks:

  • You miss real exceptions when calling methods that doesn't exist in that class. This may make more difficult to debug certain issues
  • There are icons that starts with a number, which it's invalid for ruby methods

Now, checking the new PR to upgrade the gem to 4.0.0, I noticed many new icons starting with a number. For this reason, I plan to move from this approach to a specific method in v4.0.0.

Note this is a breaking change and already existing users would need to change their code:

# v2.2.0 and before
mi.calendar

# v4.0.0
mi.shape(:calendar)
mi.shape('calendar')

Drop unicode support

Until now, all versions of this gem were released with two possible configurations:

  • Ligatures
  • Unicode

The main reason was that at the time I created it, ligatures was not completely adopted yet. However, now the support has improved a lot and browsers are already enabling it by default (See Ligatures support in "can I use?"].

Adding unicode support requires to create a separate CSS file based on the list of codepoints from the Material Icons project. This CSS file is much heavier and it should be used by default.

With a 96% of support based in the can I use? data, it's time to drop this feature for future versions of material icons.

Move CI to GitHub Actions and deprecate versions

Currently, the project is using Travis CI (org) which it's no longer supported. I plan to move the CI to GitHub actions so new PRs and commits are properly tested.

In addition to that, I plan to deprecate several ruby and rails versions as they are no longer maintained. After these changes, I will keep the following versions:

  • Ruby:
    • 2.7
    • 3.0
    • 3.1
  • Rails:
    • 5.2
    • 6.0
    • 6.1
    • 7.0

Logout icon not working

Logout icon doesn't appear when calling it.
<%= mi.logout %>
<span class="material-icons">logout</span>

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.