Skip to content

Commit

Permalink
Use html use elements for svg icons
Browse files Browse the repository at this point in the history
You can imagine this as a sort of spritemap if you will.
Icons can be referenced by id and inserted into an `svg` element with `use`.

On the jobs index page, size is reduced from 305kb to 237kb. Quite nice
  • Loading branch information
Earlopain committed Jul 16, 2024
1 parent a9a704b commit 897915f
Show file tree
Hide file tree
Showing 22 changed files with 97 additions and 75 deletions.
2 changes: 1 addition & 1 deletion app/controllers/good_job/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class ApplicationController < ActionController::Base
around_action :use_good_job_locale

content_security_policy do |policy|
policy.default_src(:none) if policy.default_src(*policy.default_src).blank?
policy.default_src(:self) if policy.default_src(*policy.default_src).blank?
policy.connect_src(:self) if policy.connect_src(*policy.connect_src).blank?
policy.base_uri(:none) if policy.base_uri(*policy.base_uri).blank?
policy.font_src(:self) if policy.font_src(*policy.font_src).blank?
Expand Down
3 changes: 3 additions & 0 deletions app/controllers/good_job/frontends_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class FrontendsController < ActionController::Base # rubocop:disable Rails/Appli
es_module_shims: GoodJob::Engine.root.join("app", "frontend", "good_job", "vendor", "es_module_shims.js"),
rails_ujs: GoodJob::Engine.root.join("app", "frontend", "good_job", "vendor", "rails_ujs.js"),
},
svg: {
icons: GoodJob::Engine.root.join("app", "frontend", "good_job", "icons.svg"),
},
}.freeze

MODULE_OVERRIDES = {
Expand Down
79 changes: 79 additions & 0 deletions app/frontend/good_job/icons.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions app/frontend/good_job/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,8 @@
.w-fit-content {
width: fit-content
}

.svg-icon {
height: 1rem;
width: 1rem;
}
13 changes: 8 additions & 5 deletions app/helpers/good_job/icons_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@ def status_icon(status, **options)
content_tag :span, icon, **options
end

def render_icon(name, **options)
# workaround to render svg icons without all of the log messages
partial = lookup_context.find_template("good_job/shared/icons/#{name}", [], true)
options[:class] = Array(options[:class]).join(" ")
partial.render(self, { class: options[:class] })
def render_icon(name, class: nil, **options)
tag.svg(viewBox: "0 0 16 16", class: "svg-icon #{binding.local_variable_get(:class)}", **options) do
tag.use(fill: "currentColor", href: "#{icons_path}##{name}")
end
end

def icons_path
@_icons_path ||= frontend_static_path(:icons, format: :svg, locale: nil)
end
end
end
5 changes: 0 additions & 5 deletions app/views/good_job/shared/icons/_arrow_clockwise.html.erb

This file was deleted.

5 changes: 0 additions & 5 deletions app/views/good_job/shared/icons/_check.html.erb

This file was deleted.

4 changes: 0 additions & 4 deletions app/views/good_job/shared/icons/_circle_half.html.erb

This file was deleted.

5 changes: 0 additions & 5 deletions app/views/good_job/shared/icons/_clock.html.erb

This file was deleted.

5 changes: 0 additions & 5 deletions app/views/good_job/shared/icons/_dash_circle.html.erb

This file was deleted.

3 changes: 0 additions & 3 deletions app/views/good_job/shared/icons/_dots.html.erb

This file was deleted.

4 changes: 0 additions & 4 deletions app/views/good_job/shared/icons/_eject.html.erb

This file was deleted.

5 changes: 0 additions & 5 deletions app/views/good_job/shared/icons/_exclamation.html.erb

This file was deleted.

3 changes: 0 additions & 3 deletions app/views/good_job/shared/icons/_globe.html.erb

This file was deleted.

4 changes: 0 additions & 4 deletions app/views/good_job/shared/icons/_info.html.erb

This file was deleted.

5 changes: 0 additions & 5 deletions app/views/good_job/shared/icons/_moon_stars_fill.html.erb

This file was deleted.

4 changes: 0 additions & 4 deletions app/views/good_job/shared/icons/_pause.html.erb

This file was deleted.

4 changes: 0 additions & 4 deletions app/views/good_job/shared/icons/_play.html.erb

This file was deleted.

4 changes: 0 additions & 4 deletions app/views/good_job/shared/icons/_skip_forward.html.erb

This file was deleted.

4 changes: 0 additions & 4 deletions app/views/good_job/shared/icons/_stop.html.erb

This file was deleted.

4 changes: 0 additions & 4 deletions app/views/good_job/shared/icons/_sun_fill.html.erb

This file was deleted.

2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@

scope :frontend, controller: :frontends, defaults: { version: GoodJob::VERSION.tr(".", "-") } do
get "modules/:version/:id", action: :module, as: :frontend_module, constraints: { format: 'js' }
get "static/:version/:id", action: :static, as: :frontend_static, constraints: { format: %w[css js] }
get "static/:version/:id", action: :static, as: :frontend_static, constraints: { format: %w[css js svg] }
end
end

0 comments on commit 897915f

Please sign in to comment.