♻️ Split the styling hook into multiple hook

This commit is contained in:
Philipp Stracker 2025-01-17 13:05:25 +01:00
parent a5ceec2af4
commit 18429d91e5
No known key found for this signature in database
10 changed files with 90 additions and 63 deletions

View file

@ -4,14 +4,13 @@ import { StylingHooks } from '../../../../../../data';
import { SelectStylingSection } from '../Layout';
const SectionButtonColor = ( { location } ) => {
const { color, setColor, colorChoices } =
StylingHooks.useStylingProps( location );
const { color, setColor, choices } = StylingHooks.useColorProps( location );
return (
<SelectStylingSection
title={ __( 'Button Color', 'woocommerce-paypal-payments' ) }
className="button-color"
options={ colorChoices }
options={ choices }
value={ color }
onChange={ setColor }
/>

View file

@ -4,14 +4,13 @@ import { StylingHooks } from '../../../../../../data';
import { SelectStylingSection } from '../Layout';
const SectionButtonLabel = ( { location } ) => {
const { label, setLabel, labelChoices } =
StylingHooks.useStylingProps( location );
const { label, setLabel, choices } = StylingHooks.useLabelProps( location );
return (
<SelectStylingSection
title={ __( 'Button Label', 'woocommerce-paypal-payments' ) }
className="button-label"
options={ labelChoices }
options={ choices }
value={ label }
onChange={ setLabel }
/>

View file

@ -4,10 +4,10 @@ import { StylingHooks } from '../../../../../../data';
import { RadiobuttonStylingSection } from '../Layout';
const SectionButtonLayout = ( { location } ) => {
const { supportsLayout, layout, setLayout, layoutChoices } =
StylingHooks.useStylingProps( location );
const { isAvailable, layout, setLayout, choices } =
StylingHooks.useLayoutProps( location );
if ( ! supportsLayout ) {
if ( ! isAvailable ) {
return null;
}
@ -15,7 +15,7 @@ const SectionButtonLayout = ( { location } ) => {
<RadiobuttonStylingSection
className="button-layout"
title={ __( 'Button Layout', 'woocommerce-paypal-payments' ) }
options={ layoutChoices }
options={ choices }
selected={ layout }
onChange={ setLayout }
/>

View file

@ -4,14 +4,13 @@ import { StylingHooks } from '../../../../../../data';
import { RadiobuttonStylingSection } from '../Layout';
const SectionButtonShape = ( { location } ) => {
const { shape, setShape, shapeChoices } =
StylingHooks.useStylingProps( location );
const { shape, setShape, choices } = StylingHooks.useShapeProps( location );
return (
<RadiobuttonStylingSection
title={ __( 'Shape', 'woocommerce-paypal-payments' ) }
className="button-shape"
options={ shapeChoices }
options={ choices }
selected={ shape }
onChange={ setShape }
/>

View file

@ -1,14 +1,11 @@
import { __, sprintf } from '@wordpress/i18n';
import { __ } from '@wordpress/i18n';
import { StylingHooks } from '../../../../../../data';
import { Description } from '../../../../../ReusableComponents/SettingsBlocks';
import { SelectStylingSection, StylingSection } from '../Layout';
const LocationSelector = ( { location, setLocation } ) => {
const { locationChoices, locationDetails } =
StylingHooks.useStylingProps( location );
const { description, link } = locationDetails || {};
const locationDescription = sprintf( description, link );
const { choices, description } = StylingHooks.useLocationProps( location );
return (
<>
@ -25,13 +22,11 @@ const LocationSelector = ( { location, setLocation } ) => {
className="location-selector"
title={ __( 'Location', 'woocommerce-paypal-payments' ) }
separatorAndGap={ false }
options={ locationChoices }
options={ choices }
value={ location }
onChange={ setLocation }
>
<Description asHtml={ true }>
{ locationDescription }
</Description>
<Description asHtml={ true }>{ description }</Description>
</SelectStylingSection>
</>
);

View file

@ -4,14 +4,14 @@ import { StylingHooks } from '../../../../../../data';
import { CheckboxStylingSection } from '../Layout';
const SectionPaymentMethods = ( { location } ) => {
const { paymentMethods, setPaymentMethods, paymentMethodChoices } =
StylingHooks.useStylingProps( location );
const { paymentMethods, setPaymentMethods, choices } =
StylingHooks.usePaymentMethodProps( location );
return (
<CheckboxStylingSection
title={ __( 'Payment Methods', 'woocommerce-paypal-payments' ) }
className="payment-methods"
options={ paymentMethodChoices }
options={ choices }
value={ paymentMethods }
onChange={ setPaymentMethods }
/>

View file

@ -4,10 +4,10 @@ import { StylingHooks } from '../../../../../../data';
import { CheckboxStylingSection } from '../Layout';
const SectionTagline = ( { location } ) => {
const { supportsTagline, tagline, setTagline, taglineChoices } =
StylingHooks.useStylingProps( location );
const { isAvailable, tagline, setTagline, choices } =
StylingHooks.useTaglineProps( location );
if ( ! supportsTagline ) {
if ( ! isAvailable ) {
return null;
}
@ -15,7 +15,7 @@ const SectionTagline = ( { location } ) => {
<CheckboxStylingSection
title={ __( 'Tagline', 'woocommerce-paypal-payments' ) }
className="tagline"
options={ taglineChoices }
options={ choices }
value={ tagline }
onChange={ setTagline }
/>

View file

@ -4,4 +4,4 @@ export { default as ButtonLabel } from './ButtonLabel';
export { default as ButtonLayout } from './ButtonLayout';
export { default as ButtonShape } from './ButtonShape';
export { default as PaymentMethods } from './PaymentMethods';
export { default as TagLine } from './TagLine';
export { default as Tagline } from './Tagline';

View file

@ -6,7 +6,7 @@ import {
ButtonShape,
ButtonLabel,
ButtonColor,
TagLine,
Tagline,
} from './Content';
const SettingsPanel = () => {
@ -23,7 +23,7 @@ const SettingsPanel = () => {
<ButtonShape location={ location } />
<ButtonLabel location={ location } />
<ButtonColor location={ location } />
<TagLine location={ location } />
<Tagline location={ location } />
</div>
);
};

View file

@ -7,7 +7,7 @@
* @file
*/
import { __ } from '@wordpress/i18n';
import { __, sprintf } from '@wordpress/i18n';
import { useCallback } from '@wordpress/element'; // Temporary
import { useDispatch, useSelect } from '@wordpress/data';
@ -82,49 +82,84 @@ export const useStylingLocation = () => {
return { location, setLocation };
};
export const useStylingProps = ( location ) => {
export const useLocationProps = ( location ) => {
const details = STYLING_LOCATIONS[ location ] ?? {};
// eslint-disable-next-line @wordpress/valid-sprintf
const description = sprintf( details.description ?? '', details.link );
return {
choices: Object.values( STYLING_LOCATIONS ),
details,
description,
};
};
export const usePaymentMethodProps = ( location ) => {
const { getLocationProp, setLocationProp } = useHooks();
return {
// Location (drop down).
locationChoices: Object.values( STYLING_LOCATIONS ),
locationDetails: STYLING_LOCATIONS[ location ],
choices: Object.values( STYLING_PAYMENT_METHODS ),
paymentMethods: getLocationProp( location, 'methods' ),
setPaymentMethods: ( methods ) =>
setLocationProp( location, 'methods', methods ),
};
};
// Payment methods (checkboxes).
paymentMethodChoices: Object.values( STYLING_PAYMENT_METHODS ),
paymentMethods: getLocationProp( 'methods' ),
setPaymentMethods: ( methods ) => setLocationProp( 'methods', methods ),
export const useColorProps = ( location ) => {
const { getLocationProp, setLocationProp } = useHooks();
// Color (dropdown).
colorChoices: Object.values( STYLING_COLORS ),
color: getLocationProp( 'color' ),
setColor: ( color ) => setLocationProp( 'color', color ),
return {
choices: Object.values( STYLING_COLORS ),
color: getLocationProp( location, 'color' ),
setColor: ( color ) => setLocationProp( location, 'color', color ),
};
};
// Shape (radio).
shapeChoices: Object.values( STYLING_SHAPES ),
shape: getLocationProp( 'shape' ),
setShape: ( shape ) => setLocationProp( 'shape', shape ),
export const useShapeProps = ( location ) => {
const { getLocationProp, setLocationProp } = useHooks();
// Label (dropdown).
labelChoices: Object.values( STYLING_LABELS ),
label: getLocationProp( 'label' ),
setLabel: ( label ) => setLocationProp( 'label', label ),
return {
choices: Object.values( STYLING_SHAPES ),
shape: getLocationProp( location, 'shape' ),
setShape: ( shape ) => setLocationProp( location, 'shape', shape ),
};
};
// Layout (radio).
layoutChoices: Object.values( STYLING_LAYOUTS ),
supportsLayout: true,
layout: getLocationProp( 'layout' ),
setLayout: ( layout ) => setLocationProp( 'layout', layout ),
export const useLabelProps = ( location ) => {
const { getLocationProp, setLocationProp } = useHooks();
// Tagline (checkbox).
taglineChoices: [
return {
choices: Object.values( STYLING_LABELS ),
label: getLocationProp( location, 'label' ),
setLabel: ( label ) => setLocationProp( location, 'label', label ),
};
};
export const useLayoutProps = ( location ) => {
const { getLocationProp, setLocationProp } = useHooks();
return {
choices: Object.values( STYLING_LAYOUTS ),
isAvailable: true,
layout: getLocationProp( location, 'layout' ),
setLayout: ( layout ) => setLocationProp( location, 'layout', layout ),
};
};
export const useTaglineProps = ( location ) => {
const { getLocationProp, setLocationProp } = useHooks();
return {
choices: [
{
value: 'tagline',
label: __( 'Enable Tagline', 'woocommerce-paypal-payments' ),
},
],
supportsTagline: true,
tagline: getLocationProp( 'tagline' ),
setTagline: ( tagline ) => setLocationProp( 'tagline', tagline ),
isAvailable: true,
tagline: getLocationProp( location, 'tagline' ),
setTagline: ( tagline ) =>
setLocationProp( location, 'tagline', tagline ),
};
};