Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document Outline: Restructured buttons to anchors #7170

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion editor/components/document-outline/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,16 @@ export const DocumentOutline = ( { blocks = [], title, onSelect, isTitleSupporte
// Select the corresponding block in the main editor
// when clicking on a heading item from the list.
const onSelectHeading = ( uid ) => onSelect( uid );
const titleNode = document.querySelector( '.editor-post-title__input' );
const titleId = () => {
if ( typeof titleNode.getAttribute( 'id' ) ) {
titleNode.setAttribute( 'id', 'id-' + titleNode.innerHTML );
Copy link
Member

@jorgefilipecosta jorgefilipecosta Jul 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are changing a dom node rendered by React directly, that may have undesirable and unexpected effects.
Is using the same events/focus mechanism we had but just change the button to an anchor an option?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that would be great too. I think I tried to do that there, how would I change it to reflect a better practice?

}

return titleNode.getAttribute( 'id' );
};
const focusTitle = () => {
// Not great but it's the simplest way to focus the title right now.
const titleNode = document.querySelector( '.editor-post-title__input' );
if ( titleNode ) {
titleNode.focus();
}
Expand All @@ -115,6 +122,7 @@ export const DocumentOutline = ( { blocks = [], title, onSelect, isTitleSupporte
level="Title"
isValid
onClick={ focusTitle }
titleId={ titleId }
>
{ title }
</DocumentOutlineItem>
Expand Down
8 changes: 5 additions & 3 deletions editor/components/document-outline/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const TableOfContentsItem = ( {
level,
onClick,
path = [],
titleId,
} ) => (
<li
className={ classnames(
Expand All @@ -29,9 +30,10 @@ const TableOfContentsItem = ( {
}
) }
>
<button
className="document-outline__button"
<a
onClick={ onClick }
className="document-outline__trigger"
href={ '#' + titleId }
>
<span className="document-outline__emdash" aria-hidden="true"></span>
{
Expand All @@ -50,7 +52,7 @@ const TableOfContentsItem = ( {
{ children }
</span>
<span className="screen-reader-text">{ __( '(Click to focus this heading)' ) }</span>
</button>
</a>
</li>
);

Expand Down
7 changes: 6 additions & 1 deletion editor/components/document-outline/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,23 @@
}
}

.document-outline__button {
.document-outline__trigger {
cursor: pointer;
background: none;
border: none;
display: flex;
align-items: flex-start;
color: $dark-gray-800;
text-align: left;
text-decoration: none;

&:focus {
@include button-style__focus-active;
}

.document-outline__item-content {
text-decoration: underline;
}
}

.document-outline__level {
Expand Down