-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #178 from 10up/feature/post-level-components
Add Post level components to allow easy building of page level blocks
- Loading branch information
Showing
70 changed files
with
2,286 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
import { dispatch } from '@wordpress/data'; | ||
import domReady from '@wordpress/dom-ready'; | ||
|
||
import { iconStore } from '../../stores'; | ||
|
||
export function registerIcons(options) { | ||
dispatch(iconStore).registerIconSet(options); | ||
domReady(() => { | ||
dispatch(iconStore).registerIconSet(options); | ||
}); | ||
} |
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,3 @@ | ||
import { createContext } from '@wordpress/element'; | ||
|
||
export const AuthorContext = createContext(); |
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,144 @@ | ||
import { useContext } from '@wordpress/element'; | ||
import { useSelect } from '@wordpress/data'; | ||
import { store as blockEditorStore } from '@wordpress/block-editor'; | ||
import PropTypes from 'prop-types'; | ||
import { AuthorContext } from './context'; | ||
|
||
/** | ||
* @typedef {object} Author | ||
* @property {object} author | ||
* @property {object<string, string>} author.avatar_urls | ||
* @property {string} author.description | ||
* @property {string} author.email | ||
* @property {string} author.first_name | ||
* @property {number} author.id | ||
* @property {string} author.last_name | ||
* @property {string} author.link | ||
* @property {string} author.name | ||
* @property {string} author.nickname | ||
* @property {string} author.registered_date | ||
* @property {string} author.slug | ||
* @property {string} author.url | ||
*/ | ||
|
||
export const Name = (props) => { | ||
const { tagName: TagName, ...rest } = props; | ||
|
||
/** | ||
* @type {Author} | ||
*/ | ||
const { name, link } = useContext(AuthorContext); | ||
|
||
const wrapperProps = { ...rest }; | ||
|
||
if (TagName === 'a' && link) { | ||
wrapperProps.href = link; | ||
} | ||
|
||
return <TagName {...wrapperProps}>{name}</TagName>; | ||
}; | ||
|
||
Name.propTypes = { | ||
tagName: PropTypes.string, | ||
}; | ||
|
||
Name.defaultProps = { | ||
tagName: 'span', | ||
}; | ||
|
||
export const FirstName = (props) => { | ||
const { tagName: TagName, ...rest } = props; | ||
|
||
/** | ||
* @type {Author} | ||
*/ | ||
const { first_name: firstName } = useContext(AuthorContext); | ||
|
||
return <TagName {...rest}>{firstName}</TagName>; | ||
}; | ||
|
||
FirstName.propTypes = { | ||
tagName: PropTypes.string, | ||
}; | ||
|
||
FirstName.defaultProps = { | ||
tagName: 'span', | ||
}; | ||
|
||
export const LastName = (props) => { | ||
const { tagName: TagName, ...rest } = props; | ||
|
||
/** | ||
* @type {Author} | ||
*/ | ||
const { last_name: lastName } = useContext(AuthorContext); | ||
|
||
return <TagName {...rest}>{lastName}</TagName>; | ||
}; | ||
|
||
LastName.propTypes = { | ||
tagName: PropTypes.string, | ||
}; | ||
|
||
LastName.defaultProps = { | ||
tagName: 'span', | ||
}; | ||
|
||
function useDefaultAvatar() { | ||
const { avatarURL: defaultAvatarUrl } = useSelect((select) => { | ||
const { getSettings } = select(blockEditorStore); | ||
const { __experimentalDiscussionSettings } = getSettings(); | ||
return __experimentalDiscussionSettings; | ||
}); | ||
return defaultAvatarUrl; | ||
} | ||
|
||
export const Avatar = (props) => { | ||
const { ...rest } = props; | ||
|
||
/** | ||
* @type {Author} | ||
*/ | ||
const authorDetails = useContext(AuthorContext); | ||
|
||
const avatarUrls = authorDetails?.avatar_urls ? Object.values(authorDetails.avatar_urls) : null; | ||
const defaultAvatar = useDefaultAvatar(); | ||
|
||
const avatarSourceUrl = avatarUrls ? avatarUrls[avatarUrls.length - 1] : defaultAvatar; | ||
|
||
return <img src={avatarSourceUrl} {...rest} />; | ||
}; | ||
|
||
export const Bio = (props) => { | ||
const { tagName: TagName = 'p', ...rest } = props; | ||
|
||
/** | ||
* @type {Author} | ||
*/ | ||
const { description } = useContext(AuthorContext); | ||
|
||
return <TagName {...rest}>{description}</TagName>; | ||
}; | ||
|
||
Bio.propTypes = { | ||
tagName: PropTypes.string, | ||
}; | ||
|
||
Bio.defaultProps = { | ||
tagName: 'p', | ||
}; | ||
|
||
export const Email = (props) => { | ||
const { ...rest } = props; | ||
|
||
/** | ||
* @type {Author} | ||
*/ | ||
const { email } = useContext(AuthorContext); | ||
|
||
return ( | ||
<a href={`mailto:${email}`} {...rest}> | ||
{email} | ||
</a> | ||
); | ||
}; |
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,74 @@ | ||
import { Children } from '@wordpress/element'; | ||
import { store as coreStore } from '@wordpress/core-data'; | ||
import { Spinner } from '@wordpress/components'; | ||
import { useSelect } from '@wordpress/data'; | ||
import PropTypes from 'prop-types'; | ||
import { usePost } from '../../hooks'; | ||
import { Name, FirstName, LastName, Avatar, Bio, Email } from '../author'; | ||
|
||
import { AuthorContext } from '../author/context'; | ||
|
||
export const PostAuthor = (props) => { | ||
const { children, ...rest } = props; | ||
const { postId, postType } = usePost(); | ||
|
||
const [author, hasResolved] = useSelect( | ||
(select) => { | ||
const { getEditedEntityRecord, getUser, hasFinishedResolution } = select(coreStore); | ||
|
||
const postQuery = ['postType', postType, postId]; | ||
|
||
const post = getEditedEntityRecord(...postQuery); | ||
const hasResolvedPost = hasFinishedResolution('getEditedEntityRecord', postQuery); | ||
|
||
const _authorId = hasResolvedPost ? post?.author : undefined; | ||
|
||
const author = getUser(_authorId); | ||
const hasResolvedAuthor = hasFinishedResolution('getUser', [_authorId]); | ||
|
||
return [author, hasResolvedAuthor && hasResolvedPost]; | ||
}, | ||
[postType, postId], | ||
); | ||
|
||
const hasRenderCallback = typeof children === 'function'; | ||
|
||
const hasChildComponents = !hasRenderCallback && Children.count(children); | ||
|
||
if (!hasResolved) { | ||
return <Spinner />; | ||
} | ||
|
||
if (hasChildComponents) { | ||
return ( | ||
<AuthorContext.Provider value={author}> | ||
<div {...rest}>{children}</div> | ||
</AuthorContext.Provider> | ||
); | ||
} | ||
|
||
if (hasRenderCallback) { | ||
return children(author); | ||
} | ||
|
||
return <Name {...rest} />; | ||
}; | ||
|
||
PostAuthor.propTypes = { | ||
children: PropTypes.oneOfType([ | ||
PropTypes.func, | ||
PropTypes.node, | ||
PropTypes.arrayOf(PropTypes.node), | ||
]), | ||
}; | ||
|
||
PostAuthor.defaultProps = { | ||
children: null, | ||
}; | ||
|
||
PostAuthor.Name = Name; | ||
PostAuthor.FirstName = FirstName; | ||
PostAuthor.LastName = LastName; | ||
PostAuthor.Avatar = Avatar; | ||
PostAuthor.Bio = Bio; | ||
PostAuthor.Email = Email; |
Oops, something went wrong.