Code Monkey home page Code Monkey logo

show-hide-section-block's Introduction

Show / Hide Section Block

Display an accessible show/hide interface with details and summary elements.

Description

Show / Hide Section Block provides two blocks: a Show / Hide Group block that is populated with one or more Show / Hide Section blocks.

The HTML output for an individual section is:

<!-- Each section block has a details element. -->
<details>
	<!-- Each details element has a summary and additional, collapsible content. -->
	<summary>Summary text</summary>
	<!-- Additional inner blocks of any type are populated here. -->
</details>

The HTML output for the group is:

<div>
	<details>
		<summary></summary>
		<!-- Additional blocks to be shown/hidden -->
	</details>
	<details>
		<summary></summary>
		<!-- Additional blocks to be shown hidden -->
	</details>
	<!-- Additional section blocks -->
</div>

An option is provided in the block's side panel to toggle an "Open all"/"Close all" button. If this is toggled on for a block, an additional <button> element will be inserted and JavaScript will be used on the front-end to control the show/hide behavior of all <details> elements in the group at once.

If the toggle is off, no JavaScript is loaded on the front end.

Changelog

2.0.3

  • Fix an issue where the front-end script was enqueued twice.

2.0.2

  • Fix an issue preventing Open All toggle from working in Chrome.
  • Update @wordpress/scripts dependency to 26.3.0.
  • Improve block organization, use of block.json.

2.0.1

  • Add a variety of supports options to block.json so that core styles can be applied as expected.
  • Improve asset loading so that the front-end script is only enqueued when the block is in use and when the the open/close all option is selected.
  • Add languages directory, POT file for translations.
  • Update @wordpress/scripts dependency to 25.5.0

2.0.0

Initial public release.

show-hide-section-block's People

Contributors

dependabot[bot] avatar jeremyfelt avatar philcable avatar slocker3 avatar webelaine avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

show-hide-section-block's Issues

Improve spacebar behavior when entering summary text

The spacebar is used on a <summary> element to toggle its parent <details> open/closed. Because of this, we do this horribly hacky thing to prevent the default behavior in the editor, but still insert a space:

/**
* Insert a space at the current position of the cursor and then adjust
* the cursor position, accounting for any selection it has made.
*
* @param {Node} node
*/
const insertSpace = ( node ) => {
const { ownerDocument } = node;
const { defaultView } = ownerDocument;
const sel = defaultView.getSelection();
const range = sel.getRangeAt( 0 );
const textNode = document.createTextNode( ' ' );
range.deleteContents();
range.insertNode( textNode );
range.setStartAfter( textNode );
};

This works, though it can be annoying because there's a slight lag before the space is inserted. If you're typing at any kind of pace at all, you may need to go back and correct the spaces.

The answer may be to use different markup in the editor, though it's nice to present the component as it will be presented on the front-end.

Maybe we could consider presenting different markup when the block is focused in the editor?

Add no-styles option

Could be a block style, or could be a plugin setting - add option to exclude all CSS from the block.

Cannot use spacebar when typing a summary

May be related to Cannot update a component (EditorProvider) while rendering a different component (edit). To locate the bad setState() call inside edit, follow the stack trace as described in https://reactjs.org/link/setstate-in-render

Ability to target elements within theme.json

My expectation is that the summary element should be available to style via theme.json.

Example:

"happyprime/show-hide-section": {
      "elements": {
	      "summary": {
		      "typography": {
			      "fontWeight": "700"
		      }
	      }
      }
},

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.