-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new Sidebar Container block to handle sticky sidebar logic (#397)
This pulls out the sticky-related JS from the table of contents block, and ports over some CSS from the child themes, so that the sidebar pattern with table of contents can be reused. By consolidating the code here, we can prevent duplicating the CSS across the child themes.
- Loading branch information
Showing
9 changed files
with
197 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
/** | ||
* Block Name: Sidebar Container | ||
* Description: A sticky container to be used in 2-column layouts. | ||
* Only added in templates (code), not enabled in the editor. | ||
* | ||
* @package wporg | ||
*/ | ||
|
||
namespace WordPressdotorg\MU_Plugins\Sidebar_Container_Block; | ||
|
||
use function WordPressdotorg\MU_Plugins\Helpers\register_assets_from_metadata; | ||
|
||
add_action( 'init', __NAMESPACE__ . '\init' ); | ||
|
||
/** | ||
* Registers the block using the metadata loaded from the `block.json` file. | ||
* Behind the scenes, it registers also all assets so they can be enqueued | ||
* through the block editor in the corresponding context. | ||
* | ||
* @see https://developer.wordpress.org/reference/functions/register_block_type/ | ||
*/ | ||
function init() { | ||
register_block_type( | ||
__DIR__ . '/build', | ||
array( | ||
'render_callback' => __NAMESPACE__ . '\render', | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Render the block content. | ||
* | ||
* @param array $attributes Block attributes. | ||
* @param string $content Block default content. | ||
* @param WP_Block $block Block instance. | ||
* | ||
* @return string Returns the block markup. | ||
*/ | ||
function render( $attributes, $content, $block ) { | ||
$back_to_top = sprintf( | ||
'<p class="has-small-font-size is-link-to-top"><a href="#wp--skip-link--target">%s</a></p>', | ||
esc_html__( '↑ Back to top', 'wporg' ) | ||
); | ||
|
||
$wrapper_attributes = get_block_wrapper_attributes(); | ||
return sprintf( | ||
'<div %1$s>%2$s%3$s</div>', | ||
$wrapper_attributes, | ||
$content, | ||
$back_to_top | ||
); | ||
} |
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,38 @@ | ||
.wp-block-wporg-sidebar-container .is-link-to-top { | ||
display: none; | ||
} | ||
|
||
/* Slot the search & table of contents into a floating sidebar on large screens. */ | ||
@media (min-width: 1200px) { | ||
.wp-block-wporg-sidebar-container { | ||
--local--block-end-sidebar--width: 340px; | ||
|
||
position: absolute; | ||
top: calc(var(--wp-global-header-offset, 0px) + var(--wp-local-header-offset, 0px)); | ||
|
||
/* Right offset should be "edge spacing" at minimum, otherwise calculate it to be centered. */ | ||
right: max(var(--wp--preset--spacing--edge-space), calc((100% - var(--wp--style--global--wide-size)) / 2)); | ||
width: var(--local--block-end-sidebar--width); | ||
margin-top: var(--wp--preset--spacing--edge-space) !important; | ||
|
||
&.is-fixed-sidebar { | ||
position: fixed; | ||
} | ||
|
||
&.is-bottom-sidebar { | ||
position: absolute; | ||
} | ||
|
||
&.is-fixed-sidebar .is-link-to-top, | ||
&.is-bottom-sidebar .is-link-to-top { | ||
display: block; | ||
} | ||
} | ||
} | ||
|
||
@media (min-width: 890px) { | ||
/* stylelint-disable selector-id-pattern */ | ||
#wp--skip-link--target { | ||
scroll-margin-top: var(--wp-local-header-offset, 0); | ||
} | ||
} |
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,19 @@ | ||
{ | ||
"$schema": "https://schemas.wp.org/trunk/block.json", | ||
"apiVersion": 2, | ||
"name": "wporg/sidebar-container", | ||
"title": "Sidebar Container", | ||
"icon": "align-pull-right", | ||
"category": "layout", | ||
"description": "A sticky container to be used in 2-column layouts.", | ||
"textdomain": "wporg", | ||
"attributes": {}, | ||
"supports": { | ||
"inserter": false, | ||
"__experimentalLayout": true | ||
}, | ||
"editorScript": "file:./index.js", | ||
"editorStyle": "file:./editor-style.css", | ||
"style": "file:./style.css", | ||
"viewScript": "file:./view.js" | ||
} |
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,25 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { registerBlockType } from '@wordpress/blocks'; | ||
import { InnerBlocks, useBlockProps } from '@wordpress/block-editor'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import metadata from './block.json'; | ||
|
||
function Edit() { | ||
return ( | ||
<div { ...useBlockProps() }> | ||
<InnerBlocks /> | ||
</div> | ||
); | ||
} | ||
|
||
registerBlockType( metadata.name, { | ||
edit: Edit, | ||
save: () => { | ||
return <InnerBlocks.Content />; | ||
}, | ||
} ); |
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
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 |
---|---|---|
|
@@ -26,6 +26,5 @@ | |
} | ||
}, | ||
"editorScript": "file:./index.js", | ||
"viewScript": "file:./view.js", | ||
"style": "file:./style.css" | ||
} |
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