-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
186 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<nav class="usa-breadcrumb" aria-label="Breadcrumbs,,"> | ||
<ol class="usa-breadcrumb__list"> | ||
<%= content %> | ||
</ol> | ||
</nav> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
class BreadcrumbComponent < ViewComponent::Base; end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<li class="usa-breadcrumb__list-item<%= current? ? ' usa-current' : '' %>"> | ||
<a href="<%= href %>" class="usa-breadcrumb__link"><span><%= content %></span></a> | ||
</li> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class BreadcrumbItemComponent < ViewComponent::Base | ||
attr_reader :href, :current | ||
alias_method :current?, :current | ||
|
||
def initialize(href:, current: false) | ||
@href = href | ||
@current = current | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
require 'action_controller' | ||
require 'action_view' | ||
require 'active_support' | ||
require 'view_component' | ||
|
||
unless defined?(Rails) | ||
module Rails | ||
class Application | ||
def routes | ||
@routes ||= Struct.new(:url_helpers).new(Module.new) | ||
end | ||
end | ||
|
||
def self.version | ||
ActionView.version.to_s | ||
end | ||
|
||
module VERSION | ||
MAJOR, MINOR = Rails.version.split('.').map(&:to_i) | ||
end | ||
|
||
def self.env | ||
@env ||= Struct.new(:development?, :test?, :production?).new(false, false, true) | ||
end | ||
|
||
def self.application | ||
@application ||= Application.new | ||
end | ||
end | ||
end | ||
|
||
Dir['_components/**/*.rb'].each { |f| require File.join('.', f) } | ||
|
||
class ComponentTag < Liquid::Block | ||
PARAM_SYNTAX = /(\w+)(?:=(?:"([^"]+?)"|(\S+)))?/.freeze | ||
|
||
def initialize(tag_name, variables, context) | ||
super | ||
|
||
@component_name, @params = variables.split(' ', 2) | ||
end | ||
|
||
# Jekyll tags don't have built-in support for variable references, but Jekyll's built-in `include` | ||
# tag has a syntax we can use as a starting point. | ||
# | ||
# @see https://github.com/jekyll/jekyll/blob/master/lib/jekyll/tags/include.rb | ||
# | ||
# Example: | ||
# | ||
# {% component link url=page.url text="Home" current %} | ||
# | ||
# In this example, `url` would be assigned from the page context variable nested value, `text` | ||
# would be assigned to a string literal, and `current` would be `true`. | ||
def parse_params(context) | ||
params = {} | ||
@params.scan(PARAM_SYNTAX) do |key, string, variable| | ||
if string | ||
params[key] = string | ||
elsif variable | ||
parts = variable.split('.') | ||
value = context | ||
while (part = parts.shift) | ||
value = value[part] | ||
end | ||
params[key] = value | ||
else | ||
params[key] = true | ||
end | ||
end | ||
params | ||
end | ||
|
||
def render(context) | ||
content = super.html_safe | ||
|
||
component_class = "#{@component_name.camelize}Component".constantize | ||
component = component_class.new(**parse_params(context).symbolize_keys).with_content(content) | ||
ActionController::Base.new.render_to_string(component) | ||
end | ||
end | ||
|
||
Liquid::Template.register_tag('component', ComponentTag) |