-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adding search widget block Co-authored-by: Rachel Cherry <bamadesigner@gmail.com> * Search block: Move away from legacy get_search_form() markup Changes the Search block to render its own search form markup, instead of the same markup that get_search_form() renders. This lets us fully match the design spec. * s/Placeholder text/Placeholder Text/ Co-Authored-By: noisysocks <robert@noisysocks.com> * Remove superfluous 'your' Co-Authored-By: noisysocks <robert@noisysocks.com> * Search: Add button and inline placeholder editing * Update CHANGELOG * Search block: Support custom CSS classes * Search block: Add aria-label to placeholder <input> * Search block: Style label as bold in theme.scss * Search block: Add aria-label to all inputs * Search block: Use ellipsis at end of input placeholders * Search block: Display placeholder in 'placeholder' attribute, not 'value' * Revert "Search block: Display placeholder in 'placeholder' attribute, not 'value'" This reverts commit 154cddc. * Search block: Remove placeholder attribute when there is a placeholder value * Search block: Fix placeholder attribute receiving false instead of undefined
- Loading branch information
1 parent
6d64856
commit 3c0b4e9
Showing
20 changed files
with
274 additions
and
0 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
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { RichText } from '@wordpress/editor'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
export default function SearchEdit( { className, attributes, setAttributes } ) { | ||
const { label, placeholder, buttonText } = attributes; | ||
|
||
return ( | ||
<div className={ className }> | ||
<RichText | ||
wrapperClassName="wp-block-search__label" | ||
aria-label={ __( 'Label text' ) } | ||
placeholder={ __( 'Add label…' ) } | ||
keepPlaceholderOnFocus | ||
formattingControls={ [] } | ||
value={ label } | ||
onChange={ ( html ) => setAttributes( { label: html } ) } | ||
/> | ||
<input | ||
className="wp-block-search__input" | ||
aria-label={ __( 'Optional placeholder text' ) } | ||
// We hide the placeholder field's placeholder when there is a value. This | ||
// stops screen readers from reading the placeholder field's placeholder | ||
// which is confusing. | ||
placeholder={ placeholder ? undefined : __( 'Optional placeholder…' ) } | ||
value={ placeholder } | ||
onChange={ ( event ) => setAttributes( { placeholder: event.target.value } ) } | ||
/> | ||
<RichText | ||
wrapperClassName="wp-block-search__button" | ||
className="wp-block-search__button-rich-text" | ||
aria-label={ __( 'Button text' ) } | ||
placeholder={ __( 'Add button text…' ) } | ||
keepPlaceholderOnFocus | ||
formattingControls={ [] } | ||
value={ buttonText } | ||
onChange={ ( html ) => setAttributes( { buttonText: html } ) } | ||
/> | ||
</div> | ||
); | ||
} |
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,26 @@ | ||
.wp-block-search { | ||
.wp-block-search__input { | ||
border-radius: $radius-round-rectangle; | ||
border: 1px solid $dark-gray-150; | ||
color: $dark-opacity-300; | ||
font-family: $default-font; | ||
font-size: $default-font-size; | ||
|
||
&:focus { | ||
outline: none; | ||
} | ||
} | ||
|
||
.wp-block-search__button { | ||
background: #f7f7f7; | ||
border-radius: $radius-round-rectangle; | ||
border: 1px solid #ccc; | ||
box-shadow: inset 0 -1px 0 #ccc; | ||
font-family: $default-font; | ||
font-size: $default-font-size; | ||
|
||
.wp-block-search__button-rich-text { | ||
padding: 6px 10px; | ||
} | ||
} | ||
} |
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,29 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import edit from './edit'; | ||
|
||
export const name = 'core/search'; | ||
|
||
export const settings = { | ||
title: __( 'Search' ), | ||
|
||
description: __( 'Help visitors find your content.' ), | ||
|
||
icon: 'search', | ||
|
||
category: 'widgets', | ||
|
||
keywords: [ __( 'find' ) ], | ||
|
||
edit, | ||
|
||
save() { | ||
return null; | ||
}, | ||
}; |
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,82 @@ | ||
<?php | ||
/** | ||
* Server-side rendering of the `core/search` block. | ||
* | ||
* @package WordPress | ||
*/ | ||
|
||
/** | ||
* Dynamically renders the `core/search` block. | ||
* | ||
* @param array $attributes The block attributes. | ||
* | ||
* @return string The search block markup. | ||
*/ | ||
function render_block_core_search( $attributes ) { | ||
static $instance_id = 0; | ||
|
||
$input_id = 'wp-block-search__input-' . ++$instance_id; | ||
|
||
if ( ! empty( $attributes['label'] ) ) { | ||
$label_markup = sprintf( | ||
'<label for="%s" class="wp-block-search__label">%s</label>', | ||
$input_id, | ||
$attributes['label'] | ||
); | ||
} | ||
|
||
$input_markup = sprintf( | ||
'<input type="search" id="%s" class="wp-block-search__input" name="s" value="%s" placeholder="%s" />', | ||
$input_id, | ||
esc_attr( get_search_query() ), | ||
esc_attr( $attributes['placeholder'] ) | ||
); | ||
|
||
if ( ! empty( $attributes['buttonText'] ) ) { | ||
$button_markup = sprintf( | ||
'<button type="submit" class="wp-block-search__button">%s</button>', | ||
$attributes['buttonText'] | ||
); | ||
} | ||
|
||
$class = 'wp-block-search'; | ||
if ( isset( $attributes['className'] ) ) { | ||
$class .= ' ' . $attributes['className']; | ||
} | ||
|
||
return sprintf( | ||
'<form class="%s" role="search" method="get" action="%s">%s</form>', | ||
$class, | ||
esc_url( home_url( '/' ) ), | ||
$label_markup . $input_markup . $button_markup | ||
); | ||
} | ||
|
||
/** | ||
* Registers the `core/search` block on the server. | ||
*/ | ||
function register_block_core_search() { | ||
register_block_type( | ||
'core/search', | ||
array( | ||
'attributes' => array( | ||
'label' => array( | ||
'type' => 'string', | ||
'default' => __( 'Search' ), | ||
), | ||
'placeholder' => array( | ||
'type' => 'string', | ||
'default' => '', | ||
), | ||
'buttonText' => array( | ||
'type' => 'string', | ||
'default' => __( 'Search' ), | ||
), | ||
), | ||
|
||
'render_callback' => 'render_block_core_search', | ||
) | ||
); | ||
} | ||
|
||
add_action( 'init', 'register_block_core_search' ); |
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,16 @@ | ||
.wp-block-search { | ||
display: flex; | ||
flex-wrap: wrap; | ||
|
||
.wp-block-search__label { | ||
width: 100%; | ||
} | ||
|
||
.wp-block-search__input { | ||
flex-grow: 1; | ||
} | ||
|
||
.wp-block-search__button { | ||
margin-left: 10px; | ||
} | ||
} |
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,5 @@ | ||
.wp-block-search { | ||
.wp-block-search__label { | ||
font-weight: bold; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
<!-- wp:search /--> |
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,10 @@ | ||
[ | ||
{ | ||
"clientId": "_clientId_0", | ||
"name": "core/search", | ||
"isValid": true, | ||
"attributes": {}, | ||
"innerBlocks": [], | ||
"originalContent": "" | ||
} | ||
] |
18 changes: 18 additions & 0 deletions
18
test/integration/full-content/fixtures/core__search.parsed.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,18 @@ | ||
[ | ||
{ | ||
"blockName": "core/search", | ||
"attrs": {}, | ||
"innerBlocks": [], | ||
"innerHTML": "", | ||
"innerContent": [] | ||
}, | ||
{ | ||
"blockName": null, | ||
"attrs": {}, | ||
"innerBlocks": [], | ||
"innerHTML": "\n", | ||
"innerContent": [ | ||
"\n" | ||
] | ||
} | ||
] |
1 change: 1 addition & 0 deletions
1
test/integration/full-content/fixtures/core__search.serialized.html
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 @@ | ||
<!-- wp:search /--> |
1 change: 1 addition & 0 deletions
1
test/integration/full-content/fixtures/core__search__custom-text.html
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 @@ | ||
<!-- wp:search {"label":"Custom label","placeholder":"Custom placeholder","buttonText":"Custom button text"} /--> |
10 changes: 10 additions & 0 deletions
10
test/integration/full-content/fixtures/core__search__custom-text.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,10 @@ | ||
[ | ||
{ | ||
"clientId": "_clientId_0", | ||
"name": "core/search", | ||
"isValid": true, | ||
"attributes": {}, | ||
"innerBlocks": [], | ||
"originalContent": "" | ||
} | ||
] |
22 changes: 22 additions & 0 deletions
22
test/integration/full-content/fixtures/core__search__custom-text.parsed.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,22 @@ | ||
[ | ||
{ | ||
"blockName": "core/search", | ||
"attrs": { | ||
"buttonText": "Custom button text", | ||
"label": "Custom label", | ||
"placeholder": "Custom placeholder" | ||
}, | ||
"innerBlocks": [], | ||
"innerHTML": "", | ||
"innerContent": [] | ||
}, | ||
{ | ||
"blockName": null, | ||
"attrs": {}, | ||
"innerBlocks": [], | ||
"innerHTML": "\n", | ||
"innerContent": [ | ||
"\n" | ||
] | ||
} | ||
] |
1 change: 1 addition & 0 deletions
1
test/integration/full-content/fixtures/core__search__custom-text.serialized.html
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 @@ | ||
<!-- wp:search /--> |