This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 218
/
block.tsx
138 lines (128 loc) · 3.53 KB
/
block.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* External dependencies
*/
import classnames from 'classnames';
import { HTMLAttributes } from 'react';
import {
useInnerBlockLayoutContext,
useProductDataContext,
} from '@woocommerce/shared-context';
import { isFeaturePluginBuild } from '@woocommerce/block-settings';
import { withProductDataContext } from '@woocommerce/shared-hocs';
import ProductName from '@woocommerce/base-components/product-name';
import { useStoreEvents } from '@woocommerce/base-context/hooks';
/**
* Internal dependencies
*/
import './style.scss';
import { Attributes } from './types';
import {
useSpacingProps,
useTypographyProps,
useColorProps,
} from '../../../../hooks/style-attributes';
type Props = Attributes & HTMLAttributes< HTMLDivElement >;
interface TagNameProps extends HTMLAttributes< HTMLOrSVGElement > {
headingLevel: number;
elementType?: keyof JSX.IntrinsicElements;
}
const TagName = ( {
children,
headingLevel,
elementType:
ElementType = `h${ headingLevel }` as keyof JSX.IntrinsicElements,
...props
}: TagNameProps ): JSX.Element => {
return <ElementType { ...props }>{ children }</ElementType>;
};
/**
* Product Title Block Component.
*
* @param {Object} props Incoming props.
* @param {string} [props.className] CSS Class name for the component.
* @param {number} [props.headingLevel] Heading level (h1, h2, etc.)
* @param {boolean} [props.showProductLink] Whether or not to display a link to the product page.
* @param {string} [props.linkTarget] Specifies where to open the linked URL.
* @param {string} [props.align] Title alignment.
* will be used if this is not provided.
* @return {*} The component.
*/
export const Block = ( props: Props ): JSX.Element => {
const {
className,
headingLevel = 2,
showProductLink = true,
linkTarget,
align,
} = props;
const { parentClassName } = useInnerBlockLayoutContext();
const { product } = useProductDataContext();
const { dispatchStoreEvent } = useStoreEvents();
const colorProps = useColorProps( props );
const spacingProps = useSpacingProps( props );
const typographyProps = useTypographyProps( props );
if ( ! product.id ) {
return (
<TagName
headingLevel={ headingLevel }
className={ classnames(
className,
colorProps.className,
'wc-block-components-product-title',
{
[ `${ parentClassName }__product-title` ]:
parentClassName,
[ `wc-block-components-product-title--align-${ align }` ]:
align && isFeaturePluginBuild(),
}
) }
style={
isFeaturePluginBuild()
? {
...spacingProps.style,
...typographyProps.style,
...colorProps.style,
}
: {}
}
/>
);
}
return (
<TagName
headingLevel={ headingLevel }
className={ classnames(
className,
colorProps.className,
'wc-block-components-product-title',
{
[ `${ parentClassName }__product-title` ]: parentClassName,
[ `wc-block-components-product-title--align-${ align }` ]:
align && isFeaturePluginBuild(),
}
) }
style={
isFeaturePluginBuild()
? {
...spacingProps.style,
...typographyProps.style,
...colorProps.style,
}
: {}
}
>
<ProductName
disabled={ ! showProductLink }
name={ product.name }
permalink={ product.permalink }
target={ linkTarget }
onClick={ () => {
dispatchStoreEvent( 'product-view-link', {
product,
} );
} }
/>
</TagName>
);
};
export default withProductDataContext( Block );