Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Component specific branding #552

Merged
merged 3 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion app/components/govuk_component/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ def initialize(classes:, html_attributes:)
end

def brand(override = nil)
override || config.brand
override || config.brand_overrides.fetch(class_prefix, config.brand)
end

# We want the main component and the subcomponents here so
# match on the second segment of the component class name
def class_prefix
# FIXME: this looks a bit dodgy...
self.class.name.match(/\w+::\w+/).to_s
end
end
25 changes: 25 additions & 0 deletions lib/govuk/components/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def reset!
# Default components configuration
#
# +:brand+ sets the value used to prefix all classes, used to allow the components to be branded for alternative (similar) design systems
# +:brand_overrides+ sets the value used to prefix classes and slots for the component class.
# +:default_back_link_text+ Default text for the back link, defaults to +Back+
# +:default_breadcrumbs_collapse_on_mobile+ false
# +:default_breadcrumbs_hide_in_print+ false
Expand Down Expand Up @@ -75,6 +76,30 @@ def reset!
# +:enable_auto_table_scopes+ automatically adds a scope of 'col' to th elements in thead and 'row' to th elements in tbody.
DEFAULTS = {
brand: 'govuk',
brand_overrides: {
# 'GovukComponent::AccordionComponent' => 'another-brand',
# 'GovukComponent::BackLinkComponent' => 'another-brand',
# 'GovukComponent::BreadcrumbsComponent' => 'another-brand',
# 'GovukComponent::CookieBannerComponent' => 'another-brand',
# 'GovukComponent::DetailsComponent' => 'another-brand',
# 'GovukComponent::ExitThisPageComponent' => 'another-brand',
# 'GovukComponent::FooterComponent' => 'another-brand',
# 'GovukComponent::HeaderComponent' => 'another-brand',
# 'GovukComponent::InsetTextComponent' => 'another-brand',
# 'GovukComponent::NotificationBannerComponent' => 'another-brand',
# 'GovukComponent::PaginationComponent' => 'another-brand',
# 'GovukComponent::PanelComponent' => 'another-brand',
# 'GovukComponent::PhaseBannerComponent' => 'another-brand',
# 'GovukComponent::SectionBreakComponent' => 'another-brand',
# 'GovukComponent::ServiceNavigationComponent' => 'another-brand',
# 'GovukComponent::StartButtonComponent' => 'another-brand',
# 'GovukComponent::SummaryListComponent' => 'another-brand',
# 'GovukComponent::TableComponent' => 'another-brand',
# 'GovukComponent::TabComponent' => 'another-brand',
# 'GovukComponent::TagComponent' => 'another-brand',
# 'GovukComponent::TaskListComponent' => 'another-brand',
# 'GovukComponent::WarningTextComponent' => 'another-brand',
},
default_back_link_text: 'Back',
default_breadcrumbs_collapse_on_mobile: false,
default_breadcrumbs_hide_in_print: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@
it_behaves_like 'a component that accepts custom classes'
it_behaves_like 'a component that accepts custom HTML attributes'
it_behaves_like 'a component that supports custom branding'
it_behaves_like 'a component that supports brand overrides'

context 'slot arguments' do
let(:slot) { :section }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,5 @@
it_behaves_like 'a component that accepts custom classes'
it_behaves_like 'a component that accepts custom HTML attributes'
it_behaves_like 'a component that supports custom branding'
it_behaves_like 'a component that supports brand overrides'
end
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,5 @@
it_behaves_like 'a component that accepts custom classes'
it_behaves_like 'a component that accepts custom HTML attributes'
it_behaves_like 'a component that supports custom branding'
it_behaves_like 'a component that supports brand overrides'
end
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@
it_behaves_like 'a component that accepts custom classes'
it_behaves_like 'a component that accepts custom HTML attributes'
it_behaves_like 'a component that supports custom branding'
it_behaves_like 'a component that supports brand overrides' do
let(:component_class_name) { "GovukComponent::CookieBannerComponent" }
end

context "when there is no text or block" do
specify "raises an appropriate error" do
Expand Down
1 change: 1 addition & 0 deletions spec/components/govuk_component/details_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
it_behaves_like 'a component that accepts custom classes'
it_behaves_like 'a component that accepts custom HTML attributes'
it_behaves_like 'a component that supports custom branding'
it_behaves_like 'a component that supports brand overrides'

context 'when text is supplied' do
before { render_inline(described_class.new(**kwargs)) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,5 @@
end

it_behaves_like 'a component that supports custom branding'
it_behaves_like 'a component that supports brand overrides'
end
1 change: 1 addition & 0 deletions spec/components/govuk_component/footer_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@
it_behaves_like 'a component that accepts custom classes'
it_behaves_like 'a component that accepts custom HTML attributes'
it_behaves_like 'a component that supports custom branding'
it_behaves_like 'a component that supports brand overrides'

describe "navigation" do
let(:custom_text) { "Some meta html" }
Expand Down
1 change: 1 addition & 0 deletions spec/components/govuk_component/header_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@
it_behaves_like 'a component that accepts custom classes'
it_behaves_like 'a component that accepts custom HTML attributes'
it_behaves_like 'a component that supports custom branding'
it_behaves_like 'a component that supports brand overrides'

context 'slot arguments' do
let(:slot) { :navigation_item }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
it_behaves_like 'a component that accepts custom classes'
it_behaves_like 'a component that accepts custom HTML attributes'
it_behaves_like 'a component that supports custom branding'
it_behaves_like 'a component that supports brand overrides'

context 'when text is supplied' do
before { render_inline(described_class.new(**kwargs)) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
let(:kwargs) { { title_text: title, text: "something", title_id: 'custom-id' } }

it_behaves_like 'a component that supports custom branding'
it_behaves_like 'a component that supports brand overrides'
end

context "when supplied with a block" do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
it_behaves_like 'a component that accepts custom classes'
it_behaves_like 'a component that accepts custom HTML attributes'
it_behaves_like 'a component that supports custom branding'
it_behaves_like 'a component that supports brand overrides'

specify "renders some page items" do
expect(rendered_content).to have_tag("nav", with: { class: component_css_class }) do
Expand Down
1 change: 1 addition & 0 deletions spec/components/govuk_component/panel_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
it_behaves_like 'a component that accepts custom classes'
it_behaves_like 'a component that accepts custom HTML attributes'
it_behaves_like 'a component that supports custom branding'
it_behaves_like 'a component that supports brand overrides'

specify 'contains a panel with the correct title and text' do
render_inline(described_class.new(**kwargs))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,7 @@
it_behaves_like 'a component that accepts custom classes'
it_behaves_like 'a component that accepts custom HTML attributes'
it_behaves_like 'a component that supports custom branding'
it_behaves_like 'a component that supports brand overrides' do
let(:extra_overrides) { ["GovukComponent::TagComponent"] }
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
it_behaves_like "a component that accepts custom classes"
it_behaves_like "a component that accepts custom HTML attributes"
it_behaves_like 'a component that supports custom branding'
it_behaves_like 'a component that supports brand overrides'

context "when visible is true" do
it "renders the section break with the visible class" do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
it_behaves_like 'a component that accepts custom classes'
it_behaves_like 'a component that accepts custom HTML attributes'
it_behaves_like 'a component that supports custom branding'
it_behaves_like 'a component that supports brand overrides'

specify 'renders a div with the expected attributes' do
expect(rendered_content).to have_tag("div", with: { class: component_css_class, 'data-module' => 'govuk-service-navigation' })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,6 @@
it_behaves_like 'a component that accepts custom classes'
it_behaves_like 'a component that accepts custom HTML attributes'
it_behaves_like 'a component that supports custom branding'
it_behaves_like 'a component that supports brand overrides'
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
it_behaves_like 'a component that accepts custom classes'
it_behaves_like 'a component that accepts custom HTML attributes'
it_behaves_like 'a component that supports custom branding'
it_behaves_like 'a component that supports brand overrides'

subject! do
render_inline(described_class.new(**kwargs)) do |component|
Expand Down
1 change: 1 addition & 0 deletions spec/components/govuk_component/tab_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@
it_behaves_like 'a component that accepts custom classes'
it_behaves_like 'a component that accepts custom HTML attributes'
it_behaves_like 'a component that supports custom branding'
it_behaves_like 'a component that supports brand overrides'

context 'slot arguments' do
let(:slot) { :tab }
Expand Down
1 change: 1 addition & 0 deletions spec/components/govuk_component/table_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,7 @@
it_behaves_like 'a component that accepts custom classes'
it_behaves_like 'a component that accepts custom HTML attributes'
it_behaves_like 'a component that supports custom branding'
it_behaves_like 'a component that supports brand overrides'

describe "column groups and columns" do
subject! do
Expand Down
3 changes: 3 additions & 0 deletions spec/components/govuk_component/tag_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,7 @@
it_behaves_like 'a component that accepts custom classes'
it_behaves_like 'a component that accepts custom HTML attributes'
it_behaves_like 'a component that supports custom branding'
it_behaves_like 'a component that supports brand overrides' do
let(:component_class) { "GovukComponent::TagComponent" }
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -372,4 +372,5 @@
it_behaves_like 'a component that accepts custom classes'
it_behaves_like 'a component that accepts custom HTML attributes'
it_behaves_like 'a component that supports custom branding'
it_behaves_like 'a component that supports brand overrides'
end
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,5 @@
it_behaves_like 'a component that accepts custom classes'
it_behaves_like 'a component that accepts custom HTML attributes'
it_behaves_like 'a component that supports custom branding'
it_behaves_like 'a component that supports brand overrides'
end
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,33 @@
expect(rendered_content).not_to match(Regexp.new(default_brand))
end
end

shared_examples 'a component that supports brand overrides' do
let(:default_brand) { 'govuk' }
let(:custom_brand) { 'carrington-institute' }

before do
class_name = defined?(component_class_name) ? component_class_name : described_class.name
extra_classes = defined?(extra_overrides) ? extra_overrides.index_with(custom_brand) : {}

Govuk::Components.configure do |conf|
conf.brand_overrides = { class_name => custom_brand, **extra_classes }
end
end

after do
Govuk::Components.reset!
end

specify "should contain the custom branding" do
render_inline(described_class.new(**kwargs))

expect(rendered_content).to match(Regexp.new(custom_brand))
end

specify "should not contain any default branding" do
render_inline(described_class.new(**kwargs))

expect(rendered_content).not_to match(Regexp.new(default_brand))
end
end
Loading