Skip to content

Commit

Permalink
Merge pull request #26 from newfold-labs/enhance/PRESS7-64-link-prefetch
Browse files Browse the repository at this point in the history
link prefetch new feature
  • Loading branch information
arunshenoy99 authored Dec 14, 2024
2 parents 150f392 + 9fc34ae commit e8537e7
Show file tree
Hide file tree
Showing 14 changed files with 802 additions and 3,654 deletions.
220 changes: 220 additions & 0 deletions components/linkPrefetch/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
import {
Toggle,
TextField,
SelectField,
Container,
} from '@newfold/ui-component-library';

const LinkPrefetch = ( { methods, constants } ) => {
const [ settings, setSettings ] = methods.useState(
methods.NewfoldRuntime.sdk.linkPrefetch.settings
);
const [ isError, setIsError ] = methods.useState( false );
const apiUrl = methods.NewfoldRuntime.createApiUrl(
'/newfold-performance/v1/link-prefetch/settings'
);

const handleChangeOption = ( option, value ) => {
if ( ! ( option in settings ) ) return;
const updatedSettings = { ...settings, [ option ]: value };
methods
.apiFetch( {
url: apiUrl,
method: 'POST',
data: { settings: updatedSettings },
} )
.then( () => {
setSettings( ( prev ) => ( { ...prev, [ option ]: value } ) );
} )
.catch( ( error ) => {
setIsError( error.message );
} );
};

methods.useUpdateEffect( () => {
methods.setStore( {
...constants.store,
linkPrefetch: settings,
} );

methods.makeNotice(
'link-prefetch-change-notice',
constants.text.linkPrefetchTitle,
isError || constants.text.linkPrefetchNoticeTitle,
isError ? 'error' : 'success',
5000
);
}, [ settings, isError ] );

return (
<>
<Container.SettingsField
title={ constants.text.linkPrefetchTitle }
description={ constants.text.linkPrefetchDescription }
>
{ /* Desktop Settings */ }
<div className="nfd-toggle-field nfd-mb-6">
<div>
<label
className="nfd-label"
htmlFor="link-prefetch-active-desktop"
>
{
constants.text
.linkPrefetchActivateOnDesktopLabel
}
</label>
<div className="nfd-select-field__description">
{
constants.text
.linkPrefetchActivateOnDesktopDescription
}
</div>
</div>
<Toggle
id="link-prefetch-active-desktop"
screenReaderLabel={
constants.text.linkPrefetchActivateOnDesktopLabel
}
checked={ settings.activeOnDesktop }
onChange={ () =>
handleChangeOption(
'activeOnDesktop',
! settings.activeOnDesktop
)
}
/>
</div>
{ settings.activeOnDesktop && (
<SelectField
id="link-prefetch-behavior"
label={ constants.text.linkPrefetchBehaviorLabel }
value={ settings.behavior }
selectedLabel={
'mouseDown' === settings.behavior
? constants.text
.linkPrefetchBehaviorMouseDownLabel
: constants.text
.linkPrefetchBehaviorMouseHoverLabel
}
onChange={ ( v ) =>
handleChangeOption( 'behavior', v )
}
description={
'mouseDown' === settings.behavior
? constants.text
.linkPrefetchBehaviorMouseDownDescription
: constants.text
.linkPrefetchBehaviorMouseHoverDescription
}
className="nfd-mb-6"
>
<SelectField.Option
label={
constants.text
.linkPrefetchBehaviorMouseHoverLabel
}
value="mouseHover"
/>
<SelectField.Option
label={
constants.text
.linkPrefetchBehaviorMouseDownLabel
}
value="mouseDown"
/>
</SelectField>
) }
{ /* Mobile Settings */ }
<div className="nfd-toggle-field nfd-mb-6">
<div>
<label
className="nfd-label"
htmlFor="link-prefetch-active-mobile"
>
{ constants.text.linkPrefetchActivateOnMobileLabel }
</label>
<div className="nfd-select-field__description">
{
constants.text
.linkPrefetchActivateOnMobileDescription
}
</div>
</div>
<Toggle
id="link-prefetch-active-mobile"
screenReaderLabel={
constants.text.linkPrefetchActivateOnMobileLabel
}
checked={ settings.activeOnMobile }
onChange={ () =>
handleChangeOption(
'activeOnMobile',
! settings.activeOnMobile
)
}
/>
</div>
{ settings.activeOnMobile && (
<SelectField
id="link-prefetch-behavior-mobile"
label={ constants.text.linkPrefetchBehaviorLabel }
value={ settings.mobileBehavior }
selectedLabel={
'touchstart' === settings.mobileBehavior
? constants.text
.linkPrefetchBehaviorMobileTouchstartLabel
: constants.text
.linkPrefetchBehaviorMobileViewportLabel
}
onChange={ ( v ) =>
handleChangeOption( 'mobileBehavior', v )
}
description={
'touchstart' === settings.mobileBehavior
? constants.text
.linkPrefetchBehaviorMobileTouchstartDescription
: constants.text
.linkPrefetchBehaviorMobileViewportDescription
}
className="nfd-mb-6"
>
<SelectField.Option
label={
constants.text
.linkPrefetchBehaviorMobileTouchstartLabel
}
value="touchstart"
/>
<SelectField.Option
label={
constants.text
.linkPrefetchBehaviorMobileViewportLabel
}
value="viewport"
/>
</SelectField>
) }
{ /* Ignore Keywords */ }
{ ( settings.activeOnMobile || settings.activeOnDesktop ) && (
<TextField
id="link-prefetch-ignore-keywords"
label={ constants.text.linkPrefetchIgnoreKeywordsLabel }
description={
constants.text.linkPrefetchIgnoreKeywordsDescription
}
onChange={ ( e ) =>
handleChangeOption(
'ignoreKeywords',
e.target.value
)
}
value={ settings.ignoreKeywords }
/>
) }
</Container.SettingsField>
</>
);
};

export default LinkPrefetch;
70 changes: 70 additions & 0 deletions components/performance/defaultText.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,76 @@ const defaultText = {
),
clearCacheNoticeTitle: __( 'Cache cleared', 'wp-module-performance' ),
clearCacheTitle: __( 'Clear Cache', 'wp-module-performance' ),
linkPrefetchDescription: __(
'Asks the browser to download and cache links on the page ahead of them being clicked on, so that when they are clicked they load almost instantly. ',
'wp-module-performance'
),
linkPrefetchNoticeTitle: __(
'Link prefetching setting saved',
'wp-module-performance'
),
linkPrefetchTitle: __( 'Link Prefetch', 'wp-module-performance' ),
linkPrefetchActivateOnDesktopDescription: __(
'Enable link prefetching on desktop',
'wp-module-performance'
),
linkPrefetchActivateOnDesktopLabel: __(
'Activate on desktop',
'wp-module-performance'
),
linkPrefetchBehaviorDescription: __(
'Behavior of the prefetch',
'wp-module-performance'
),
linkPrefetchBehaviorLabel: __( 'Behavior', 'wp-module-performance' ),
linkPrefetchBehaviorMouseDownLabel: __(
'Prefetch on Mouse down',
'wp-module-performance'
),
linkPrefetchBehaviorMouseDownDescription: __(
'Prefetch on Mouse Down: Starts loading the page as soon as you click down, for faster response when you release the click.',
'wp-module-performance'
),
linkPrefetchBehaviorMouseHoverLabel: __(
'Prefetch on Mouse Hover (Recommended)',
'wp-module-performance'
),
linkPrefetchBehaviorMouseHoverDescription: __(
'Prefetch on Mouse Hover: Begins loading the page the moment your cursor hovers over a link',
'wp-module-performance'
),
linkPrefetchActivateOnMobileDescription: __(
'Enable link prefetching on Mobile',
'wp-module-performance'
),
linkPrefetchActivateOnMobileLabel: __(
'Activate on mobile',
'wp-module-performance'
),
linkPrefetchBehaviorMobileTouchstartLabel: __(
'Prefetch on Touchstart (Recommended)',
'wp-module-performance'
),
linkPrefetchBehaviorMobileTouchstartDescription: __(
'Prefetch on Touch Start: Instantly starts loading the page as soon as you tap the screen, ensuring a quicker response when you lift your finger.',
'wp-module-performance'
),
linkPrefetchBehaviorMobileViewportLabel: __(
'Prefetch Above the Fold',
'wp-module-performance'
),
linkPrefetchBehaviorMobileViewportDescription: __(
"Prefetch Above the Fold: Loads links in your current view instantly, ensuring they're ready when you need them.",
'wp-module-performance'
),
linkPrefetchIgnoreKeywordsDescription: __(
'Exclude Keywords: A comma separated list of words or strings that will exclude a link from being prefetched. For example, excluding "app" will prevent https://example.com/apple from being prefetched.',
'wp-module-performance'
),
linkPrefetchIgnoreKeywordsLabel: __(
'Exclude keywords',
'wp-module-performance'
),
performanceAdvancedSettingsTitle: __(
'Advanced settings',
'wp-module-performance'
Expand Down
4 changes: 4 additions & 0 deletions components/performance/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { default as CacheSettings } from '../cacheSettings/';
import { default as ClearCache } from '../clearCache/';
import { default as AdvancedSettings } from '../advancedSettings';
import { default as defaultText } from './defaultText';
import { default as LinkPrefetch } from '../linkPrefetch/';

/**
* Performance Module
Expand Down Expand Up @@ -60,6 +61,9 @@ const Performance = ( { methods, constants, Components, ...props } ) => {
>
<ClearCache methods={ methods } constants={ constants } />
</Container.Block>
<Container.Block className={ 'newfold-link-prefetch' }>
<LinkPrefetch methods={ methods } constants={ constants } />
</Container.Block>
<Container.Block
separator={ true }
className={ 'newfold-performance-advanced-settings' }
Expand Down
Loading

0 comments on commit e8537e7

Please sign in to comment.