Skip to content

Commit

Permalink
Merge pull request #2104 from tf/nav-palette-color
Browse files Browse the repository at this point in the history
Accent color palettes for default navigation
  • Loading branch information
tf authored May 7, 2024
2 parents 7e6605e + ad7d15b commit a64bcd7
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
de:
pageflow:
editor:
widgets:
attributes:
defaultNavigation:
accentColor:
label: "Akzentfarbe"
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
en:
pageflow:
editor:
widgets:
attributes:
defaultNavigation:
accentColor:
label: "Accent color"
Original file line number Diff line number Diff line change
Expand Up @@ -3114,6 +3114,57 @@ describe('ScrolledEntry', () => {
expect(values).toEqual(['brand-blue', 'brand-green']);
});

it('supports named palettes', () => {
const entry = factories.entry(
ScrolledEntry,
{},
{
entryTypeSeed: normalizeSeed({
themeOptions: {
properties: {
root: {
paletteColorBrandBlue: '#00f',
paletteColorBrandGreen: '#0f0',
paletteColorAccentColor: '#123)'
}
},
palettes: {
brandColors: ['brandBlue', 'brand_green']
}
}
})
}
);

const [values] = entry.getPaletteColors({name: 'brandColors'});

expect(values).toEqual(['brand-blue', 'brand-green']);
});

it('returns empty array if named palette is missing', () => {
const entry = factories.entry(
ScrolledEntry,
{},
{
entryTypeSeed: normalizeSeed({
themeOptions: {
properties: {
root: {
paletteColorBrandBlue: '#00f',
paletteColorBrandGreen: '#0f0',
paletteColorAccentColor: '#123)'
}
}
}
})
}
);

const [values] = entry.getPaletteColors({name: 'brandColors'});

expect(values).toEqual([]);
});

describe('with shared translations', () => {
const commonPrefix = 'pageflow_scrolled.editor.palette_colors'

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React from 'react';

import {DefaultNavigation} from 'widgets/defaultNavigation/DefaultNavigation';

import {renderInEntry} from 'pageflow-scrolled/testHelpers';
import '@testing-library/jest-dom/extend-expect';

describe('DefaultNavigation', () => {
it('does not have style attribute on header by default', async () => {
const {container} = renderInEntry(
<DefaultNavigation configuration={{}} />
);

expect(container.querySelector('header')).not.toHaveAttribute('style');
});

it('supports overriding accent color', async () => {
const {container} = renderInEntry(
<DefaultNavigation configuration={{accentColor: 'brand-blue'}} />
);

expect(container.querySelector('header')).toHaveStyle(
{'--theme-accent-color': 'var(--theme-palette-color-brand-blue)'
});
});
});
14 changes: 14 additions & 0 deletions entry_types/scrolled/package/src/editor/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {SideBarController} from './controllers/SideBarController';

import {browser} from 'pageflow/frontend';
import {CheckBoxInputView, ConfigurationEditorView} from 'pageflow/ui';
import {ColorSelectInputView} from './views/inputs/ColorSelectInputView';
import {BrowserNotSupportedView} from './views/BrowserNotSupportedView';

editor.registerEntryType('scrolled', {
Expand Down Expand Up @@ -53,7 +54,20 @@ editor.widgetTypes.registerRole('header', {
editor.widgetTypes.register('defaultNavigation', {
configurationEditorView: ConfigurationEditorView.extend({
configure: function() {
const [values, texts] = this.options.entry.getPaletteColors({name: 'accentColors'});

this.tab('defaultNavigation', function() {
if (values.length) {
this.input('accentColor', ColorSelectInputView, {
includeBlank: true,
blankTranslationKey: 'pageflow_scrolled.editor.' +
'common_content_element_attributes.' +
'palette_color.blank',
values,
texts,
});
}

this.input('hideToggleMuteButton', CheckBoxInputView);
this.input('hideSharingButton', CheckBoxInputView);
this.input('fixedOnDesktop', CheckBoxInputView);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,15 @@ export const ScrolledEntry = Entry.extend({
return [values, texts]
},

getPaletteColors() {
const values = Object.keys(
this.scrolledSeed.config.theme.options.properties?.root || {}
).filter(
key => key.indexOf('paletteColor') === 0
getPaletteColors({name} = {}) {
const themeOptions = this.scrolledSeed.config.theme.options

const values = (
name ?
(themeOptions.palettes?.[name] || []) :
Object.keys(themeOptions.properties?.root || {}).filter(
key => key.indexOf('paletteColor') === 0
)
).map(
key => dasherize(key.replace('paletteColor', ''))
);
Expand Down Expand Up @@ -229,7 +233,7 @@ export const ScrolledEntry = Entry.extend({
function dasherize(text) {
return (
text[0] +
text.slice(1).replace(/[A-Z]/g, match => `-${match}`)
text.slice(1).replace('_', '-').replace(/[A-Z]/g, match => `-${match}`)
).toLowerCase();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import classNames from 'classnames';

import {
Widget,
paletteColor,
useScrollPosition,
useChapters,
useCurrentChapter,
Expand Down Expand Up @@ -125,7 +126,7 @@ export function DefaultNavigation({configuration}) {
!mobileNavHidden
),
[styles.hasChapters]: hasChapters
})}>
})} style={{'--theme-accent-color': paletteColor(configuration.accentColor)}}>
<div className={styles.navigationBarContentWrapper}>
{hasChapters && <HamburgerIcon onClick={handleBurgerMenuClick}
mobileNavHidden={mobileNavHidden}/>}
Expand Down
10 changes: 8 additions & 2 deletions package/spec/editor/views/EditWidgetView-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@ describe('EditWidgetView', () => {
var f = support.factories;

it('renders widget configuration editor', () => {
var entry = f.entry({id: 25});
var widgetTypes = f.widgetTypes([
{name: 'default_bar', role: 'navigation'},
{name: 'fancy_bar', role: 'navigation'}
], function(w) {
w.register('fancy_bar', {
configurationEditorView: Backbone.View.extend({
className: 'edit_fancy_bar'
className: 'edit_fancy_bar',

initialize() {
this.$el.attr('data-entry-id', this.options.entry.id)
}
})
});
});
Expand All @@ -24,11 +29,12 @@ describe('EditWidgetView', () => {
}, {widgetTypes: widgetTypes});
var view = new EditWidgetView({
model: widget,
entry,
widgetTypes: widgetTypes
});

view.render();

expect(view.$el.find('.edit_fancy_bar').length).toBe(1);
expect(view.$el.find('.edit_fancy_bar[data-entry-id=25]').length).toBe(1);
});
});
1 change: 1 addition & 0 deletions package/src/editor/controllers/SidebarController.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export const SidebarController = Marionette.Controller.extend({

widget: function(id) {
this.region.show(new EditWidgetView({
entry: this.entry,
model: this.entry.widgets.get(id)
}));
},
Expand Down
1 change: 1 addition & 0 deletions package/src/editor/views/EditWidgetView.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const EditWidgetView = Marionette.ItemView.extend({
onRender: function() {
var configurationEditor = this.model.widgetType().createConfigurationEditorView({
model: this.model.configuration,
entry: this.options.entry,
tab: this.options.tab
});

Expand Down

0 comments on commit a64bcd7

Please sign in to comment.