Skip to content

Commit 4108f61

Browse files
feat: support multiple sidebars (#82)
* feat: register sidebar for aside * feat: basic primary sidebar styles * refactor: adjust widget registration and styles - register and add widget areas to context in same file - rename widgets/widget areas - remove widget area titles - adjust css for sidebar widget area
1 parent c55c5bc commit 4108f61

File tree

8 files changed

+62
-18
lines changed

8 files changed

+62
-18
lines changed

src/php/functions.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@
7474
*/
7575
function add_to_context( $context ) {
7676
$context['menu'] = new \Timber\Menu( 'primary' );
77-
$context['footer_sidebar'] = Timber\Timber::get_widgets( 'footer-area' );
7877
return $context;
7978
}
8079
add_filter( 'timber/context', 'add_to_context' );

src/php/inc/theme-widgets.php

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,40 @@
66
*/
77

88
/**
9-
* Register widget area.
9+
* Register widgetized areas.
1010
*/
1111
function sparkpress_theme_widgets_init() {
12+
register_sidebar(
13+
array(
14+
'name' => 'Primary Sidebar',
15+
'id' => 'primary-sidebar',
16+
'description' => 'Widget for the sidebar area.',
17+
'before_widget' => '<div>',
18+
'after_widget' => '</div>',
19+
)
20+
);
21+
1222
register_sidebar(
1323
array(
1424
'name' => 'Footer',
1525
'id' => 'footer-area',
16-
'description' => 'Sidebar for a the footer area.',
26+
'description' => 'Widget for the footer area.',
1727
'before_widget' => '<div>',
1828
'after_widget' => '</div>',
19-
'before_title' => '<h3>',
20-
'after_title' => '</h3>',
2129
)
2230
);
2331
}
2432
add_action( 'widgets_init', 'sparkpress_theme_widgets_init' );
33+
34+
/**
35+
* Adds sidebar and footer widgets to the Timber context.
36+
*
37+
* @param array $context The Timber context array.
38+
* @return array The updated Timber context array with sidebar and footer widgets.
39+
*/
40+
function sparkpress_add_widgets_to_context( $context ) {
41+
$context['sidebar_widget'] = Timber\Timber::get_widgets( 'primary-sidebar' );
42+
$context['footer_widget'] = Timber\Timber::get_widgets( 'footer-area' );
43+
return $context;
44+
}
45+
add_filter( 'timber/context', 'sparkpress_add_widgets_to_context' );

src/php/views/base.twig

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,19 @@
88
</div>
99
{% endif %}
1010

11-
{# content block from child templates #}
12-
{% block content %}
13-
<div class="obj-width-limiter">
14-
Sorry, no content
15-
</div>
16-
{% endblock %}
17-
18-
{% if sidebar %}
19-
<aside>
20-
{{ sidebar }}
21-
</aside>
22-
{% endif %}
11+
<div class="obj-width-limiter cmp-post-body">
12+
{# content block from child templates #}
13+
{% block content %}
14+
<div class="obj-width-limiter">
15+
Sorry, no content
16+
</div>
17+
{% endblock %}
18+
{% if sidebar_widget %}
19+
<aside class="obj-width-limiter cmp-primary-sidebar">
20+
{{ sidebar_widget }}
21+
</aside>
22+
{% endif %}
23+
</div>
2324
</article>
2425

2526
{% include 'footer.twig' %}

src/php/views/footer.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
Privacy Policy
1313
</a>
1414
</p>
15-
{{ footer_sidebar }}
15+
{{ footer_widget }}
1616
</div>
1717
</footer>
1818

src/scss/base.scss

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@
6363
// Specific UI components. This is where majority of our work takes place
6464
// and our UI components are often composed of Objects and Components
6565
@import 'components/main-nav';
66+
@import 'components/primary-sidebar';
67+
@import 'components/post-body';
6668
@import 'components/skip-to-content';
6769
@import 'components/sticky-footer';
6870

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.cmp-post-body {
2+
display: flex;
3+
flex-direction: column;
4+
gap: 1.5rem 3.75rem;
5+
6+
@media (min-width: 50rem) {
7+
flex-direction: row;
8+
}
9+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.cmp-primary-sidebar {
2+
background-color: var(--gray-background-color);
3+
padding: 1rem;
4+
5+
@media (min-width: 50rem) {
6+
flex-basis: 18.75rem;
7+
}
8+
}

src/scss/elements/_root.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
--black: hsl(0deg 0% 0%);
99
--white: hsl(0deg 0% 100%);
1010

11+
// Gray
12+
--gray: hsl(0deg 0% 97%);
13+
1114
// Link colors
1215
--link-color: var(--primary-color);
1316
--link-color-hover: var(--secondary-color);
@@ -16,6 +19,7 @@
1619
// Foreground/background colors
1720
--text-color: var(--black);
1821
--background-color: var(--white);
22+
--gray-background-color: var(--gray);
1923

2024
color: var(--text-color);
2125
background-color: var(--background-color);

0 commit comments

Comments
 (0)