How to use the @wordpress/element.useContext function in @wordpress/element

To help you get started, we’ve selected a few @wordpress/element examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ampproject / amp-wp / assets / src / setup / components / reader-themes-context-provider.js View on Github external
export function ReaderThemesContextProvider( { wpAjaxUrl, children, readerThemesEndpoint, updatesNonce } ) {
	const [ themes, setThemes ] = useState( null );
	const [ fetchingThemes, setFetchingThemes ] = useState( false );
	const [ downloadingTheme, setDownloadingTheme ] = useState( false );

	const { setError } = useError();

	const { options, savingOptions } = useContext( Options );
	const { reader_theme: readerTheme, theme_support: themeSupport } = options || {};

	// This component sets state inside async functions. Use this ref to prevent state updates after unmount.
	const hasUnmounted = useRef( false );

	const selectedTheme = useMemo(
		() => themes ? themes.find( ( { slug } ) => slug === readerTheme ) : null,
		[ readerTheme, themes ],
	);

	/**
	 * Downloads the selected reader theme, if necessary, when options are saved.
	 */
	useEffect( () => {
		if ( ! selectedTheme ) {
			return;
github Automattic / vip-go-mu-plugins-built / jetpack / extensions / extended-blocks / paid-blocks / media-replace-flow.js View on Github external
MediaReplaceFlow => props => {
		const { name } = useBlockEditContext();
		const usableBlocksProps = getUsableBlockProps( name );

		const preUploadFile = useRef();
		if ( ! usableBlocksProps?.mediaReplaceFlow || ! isUpgradable( name ) ) {
			return ;
		}

		const { fileType } = usableBlocksProps;
		const onBannerVisibilityChange = useContext( PaidBlockContext );

		return (
			 {
					preUploadFile.current = files?.length ? files[ 0 ] : null;
					onBannerVisibilityChange( files?.length && isFileOfType( files[ 0 ], fileType ) );
				} }
				createNotice={ ( status, msg, options ) => {
					// Detect video file from callback and reference instance.
					if ( isFileOfType( preUploadFile.current, fileType ) ) {
						preUploadFile.current = null; // clean up the file reference.

						// Do not show Error notice when it's a video file.
						return null;
					}
github ampproject / amp-wp / assets / src / settings-page / mobile-redirection.js View on Github external
export function MobileRedirection() {
	const { editedOptions } = useContext( Options );

	const { theme_support: themeSupport } = editedOptions || {};

	// Don't show if the mode is standard or the themeSupport is not yet set.
	if ( ! themeSupport || 'standard' === themeSupport ) {
		return null;
	}

	return (
		<section>
			<h2>
				{ __( 'Mobile Redirection', 'amp' ) }
			</h2>
			
		</section>
	);
github ampproject / amp-wp / assets / src / setup / setup-wizard.js View on Github external
export function SetupWizard( { exitLink } ) {
	const { activePageIndex, currentPage: { title, PageComponent, showTitle }, moveBack, moveForward, pages } = useContext( Navigation );

	const PageComponentWithSideEffects = useMemo( () =&gt; () =&gt; (
		
			
		
	// eslint-disable-next-line react-hooks/exhaustive-deps
	), [ PageComponent ] );

	return (
		<div>
			<div>
				<div>
					
					<div>
						{ __( 'Official AMP Plugin for WordPress', 'amp' ) }
					</div></div></div></div>
github ampproject / amp-wp / assets / src / settings-page / settings-footer.js View on Github external
export function SettingsFooter() {
	const { didSaveOptions, editedOptions, hasOptionsChanges, savingOptions } = useContext( Options );
	const { downloadingTheme } = useContext( ReaderThemes );

	const { reader_theme: readerTheme, theme_support: themeSupport } = editedOptions;

	const disabled = ! themeSupport ||
		! hasOptionsChanges ||
		savingOptions ||
		didSaveOptions ||
		downloadingTheme ||
		( 'reader' === themeSupport &amp;&amp; ! readerTheme );

	return (
		<section>
			<button type="submit" disabled="{">
				{ __( 'Save changes', 'amp' ) }
				<svg viewBox="0 0 64 64" xmlns="http://www.w3.org/2000/svg"></svg></button></section>
github ampproject / amp-wp / assets / src / components / themes-api-error / index.js View on Github external
export function ThemesAPIError() {
	const { themesAPIError } = useContext( ReaderThemes );

	if ( ! themesAPIError ) {
		return null;
	}

	return (
		
			<p>
				{
					themesAPIError
				}
			</p>
		
	);
}
github ampproject / amp-wp / assets / src / onboarding-wizard / pages / save / index.js View on Github external
function Preview() {
	const [ iframeLoaded, setIframeLoaded ] = useState( false );

	const {
		editedOptions: { theme_support: themeSupport },
		originalOptions: { preview_permalink: previewPermalink, reader_theme: readerTheme },
	} = useContext( Options );

	const opacity = iframeLoaded ? '1' : '0';

	return (
		&lt;&gt;
github ampproject / amp-wp / assets / src / onboarding-wizard / components / template-mode-override-context-provider.js View on Github external
export function TemplateModeOverrideContextProvider( { children } ) {
	const { editedOptions, originalOptions, updateOptions, readerModeWasOverridden, setReaderModeWasOverridden } = useContext( Options );
	const { currentPage: { slug: currentPageSlug } } = useContext( Navigation );
	const { selectedTheme, currentTheme } = useContext( ReaderThemes );
	const { developerToolsOption, fetchingUser, originalDeveloperToolsOption } = useContext( User );
	const [ respondedToDeveloperToolsOptionChange, setRespondedToDeveloperToolsOptionChange ] = useState( false );
	const [ mostRecentlySelectedThemeSupport, setMostRecentlySelectedThemeSupport ] = useState( null );
	const [ technicalQuestionChangedAtLeastOnce, setTechnicalQuestionChangedAtLeastOnce ] = useState( false );

	const { theme_support: themeSupport } = editedOptions || {};
	const { theme_support: originalThemeSupport } = originalOptions || {};

	const technicalQuestionChanged = ! fetchingUser && developerToolsOption !== originalDeveloperToolsOption;

	/**
	 * Persist the "previously selected" note if the technical question is changed, even if it is subsequently restored.
	 */
	useEffect( () => {
		if ( technicalQuestionChanged ) {
			setTechnicalQuestionChangedAtLeastOnce( true );
github WordPress / gutenberg / packages / core-data / src / entity-provider.js View on Github external
export function useEntityId( kind, type ) {
	return useContext( getEntity( kind, type ).context );
}
github WordPress / gutenberg / packages / components / src / slot-fill / context.js View on Github external
export const useSlot = ( name ) => {
	const { getSlot, subscribe } = useContext( SlotFillContext );
	const [ slot, setSlot ] = useState( getSlot( name ) );

	useEffect( () => {
		setSlot( getSlot( name ) );
		const unsubscribe = subscribe( () => {
			setSlot( getSlot( name ) );
		} );

		return unsubscribe;
	}, [ name ] );

	return slot;
};