forked from WordPress/Learn
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add script to fix the issue (WordPress#2817)
- Loading branch information
Showing
5 changed files
with
95 additions
and
6 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
4 changes: 4 additions & 0 deletions
4
wp-content/themes/pub/wporg-learn-2024/src/lesson-standalone/block.json
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,4 @@ | ||
{ | ||
"name": "wporg-learn/lesson-standalone", | ||
"viewScript": "file:./view.js" | ||
} |
40 changes: 40 additions & 0 deletions
40
wp-content/themes/pub/wporg-learn-2024/src/lesson-standalone/index.php
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,40 @@ | ||
<?php | ||
/** | ||
* Adds scripts for lesson-standalone pattern. | ||
*/ | ||
|
||
namespace WordPressdotorg\Theme\Learn_2024\Lesson_Standalone; | ||
|
||
defined( 'WPINC' ) || die(); | ||
|
||
/** | ||
* Actions and filters. | ||
*/ | ||
add_action( 'init', __NAMESPACE__ . '\init' ); | ||
|
||
/** | ||
* Add a script to update the --sensei-wpadminbar-offset dynamically to make the standalone lesson layout correct. | ||
* | ||
* The dependencies are autogenerated in block.json, and can be read with | ||
* `wp_json_file_decode` & `register_block_script_handle. | ||
*/ | ||
function init() { | ||
$metadata_file = dirname( dirname( __DIR__ ) ) . '/build/lesson-standalone/block.json'; | ||
$metadata = wp_json_file_decode( $metadata_file, array( 'associative' => true ) ); | ||
$metadata['file'] = $metadata_file; | ||
|
||
$script_handle = register_block_script_handle( $metadata, 'viewScript', 0 ); | ||
|
||
// Enqueue the assets when the sensei lesson standalone pattern is on the page. | ||
add_action( | ||
'render_block_core/pattern', | ||
function( $block_content, $block ) use ( $script_handle ) { | ||
if ( isset( $block['attrs']['slug'] ) && 'wporg-learn-2024/sensei-lesson-standalone' === $block['attrs']['slug'] ) { | ||
wp_enqueue_script( $script_handle ); | ||
} | ||
return $block_content; | ||
}, | ||
10, | ||
2 | ||
); | ||
} |
41 changes: 41 additions & 0 deletions
41
wp-content/themes/pub/wporg-learn-2024/src/lesson-standalone/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,41 @@ | ||
// Original source: https://github.com/Automattic/sensei/blob/trunk/assets/course-theme/adminbar-layout.ts | ||
/** | ||
* External dependencies | ||
*/ | ||
import debounce from 'lodash/debounce'; | ||
|
||
/** | ||
* Track how much space the WordPress admin bar takes up at the top of the screen. | ||
* Updates a CSS variable with the value. | ||
*/ | ||
const trackAdminbarOffset = () => { | ||
const adminbar = document.querySelector( '#wpadminbar' ); | ||
if ( ! adminbar ) { | ||
return; | ||
} | ||
|
||
updateAdminbarOffset(); | ||
window.addEventListener( 'scroll', updateAdminbarOffset, { | ||
capture: false, | ||
passive: true, | ||
} ); | ||
|
||
/** | ||
* The debounce has 2 reasons here: | ||
* 1. Reduce the number of times we call the function in a resize. | ||
* 2. The admin bar contains an animated transition, so this transition | ||
* needs to be completed in order to make the correct calc. | ||
*/ | ||
window.addEventListener( 'resize', debounce( updateAdminbarOffset, 500 ) ); | ||
|
||
function updateAdminbarOffset() { | ||
if ( ! adminbar ) { | ||
return; | ||
} | ||
const { top, height } = adminbar.getBoundingClientRect(); | ||
const offset = Math.max( 0, height + top ); | ||
document.documentElement.style.setProperty( '--sensei-wpadminbar-offset', offset + 'px' ); | ||
} | ||
}; | ||
|
||
window.addEventListener( 'DOMContentLoaded', trackAdminbarOffset ); |