Skip to content

Commit 3016325

Browse files
fix(NotificationPlugin): fix offset (#6087)
* feat(NotificationPlugin): support custom offset * feat: adjust offset * chore: stash changelog [ci skip] --------- Co-authored-by: tdesign-bot <tdesign@tencent.com>
1 parent cb2775f commit 3016325

File tree

3 files changed

+32
-13
lines changed

3 files changed

+32
-13
lines changed

packages/components/notification/notification-list.tsx

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ import { usePrefixClass } from '@tdesign/shared-hooks';
66

77
export default defineComponent({
88
props: {
9+
/**
10+
* 相对于 placement 的偏移量,示例:[-10, 20] 或 ['10em', '8rem']
11+
*/
12+
offset: Array<string | number>,
13+
914
placement: {
1015
type: String,
1116
default: 'top-right',
@@ -17,15 +22,27 @@ export default defineComponent({
1722
setup(props, { expose }) {
1823
const COMPONENT_NAME = usePrefixClass('notification-list');
1924

20-
const { placement } = props as NotificationOptions;
25+
const { placement, offset } = props as NotificationOptions;
2126

2227
const list: Ref<NotificationOptions[]> = ref([]);
2328
const notificationList = ref([]);
2429

25-
const styles = computed(() => ({
26-
zIndex: DEFAULT_Z_INDEX,
27-
...PLACEMENT_OFFSET[placement],
28-
}));
30+
const styles = computed(() => {
31+
const style: CSSProperties = {
32+
zIndex: DEFAULT_Z_INDEX,
33+
...PLACEMENT_OFFSET[placement],
34+
};
35+
36+
if (Array.isArray(offset) && offset.length === 2) {
37+
const horizontalProp = placement.includes('left') ? 'left' : 'right';
38+
const verticalProp = placement.includes('top') ? 'top' : 'bottom';
39+
40+
style[horizontalProp as 'left' | 'right'] = getOffset(offset[0]) ?? style[horizontalProp as 'left' | 'right'];
41+
style[verticalProp as 'top' | 'bottom'] = getOffset(offset[1]) ?? style[verticalProp as 'top' | 'bottom'];
42+
}
43+
44+
return style;
45+
});
2946

3047
const add = (options: TdNotificationProps): number => {
3148
list.value.push(options);
@@ -45,15 +62,11 @@ export default defineComponent({
4562
return isNaN(Number(val)) ? val : `${val}px`;
4663
};
4764

48-
const notificationStyles = (item: { offset: NotificationOptions['offset']; zIndex: number }) => {
65+
const notificationStyles = (item: { zIndex: number }) => {
4966
const styles: CSSProperties = {
5067
marginBottom: DISTANCE,
5168
};
52-
if (item.offset) {
53-
styles.position = 'relative';
54-
styles.left = getOffset(item.offset[0]);
55-
styles.top = getOffset(item.offset[1]);
56-
}
69+
5770
if (item.zIndex) styles['z-index'] = item.zIndex;
5871
return styles;
5972
};
@@ -91,10 +104,9 @@ export default defineComponent({
91104

92105
return () => {
93106
if (!list.value.length) return;
94-
95107
return (
96108
<div class={`${COMPONENT_NAME.value}__show`} style={styles.value}>
97-
{list.value.map((item: { offset: NotificationOptions['offset']; zIndex: number; id: number }, index) => (
109+
{list.value.map((item: { zIndex: number; id: number }, index) => (
98110
<Notification ref={addChild} key={item.id} style={notificationStyles(item)} {...getProps(index, item)} />
99111
))}
100112
</div>

packages/components/notification/plugin.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const NotificationFunction = (options: NotificationOptions, context?: AppContext
4141

4242
const instance = createVNode(NotificationList, {
4343
placement: hackOptions.placement,
44+
offset: hackOptions.offset,
4445
});
4546

4647
// eslint-disable-next-line no-underscore-dangle
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
pr_number: 6087
3+
contributor: liweijie0812
4+
---
5+
6+
- fix(NotificationPlugin): 优化 `offset` 没有对容器层生效的问题 @liweijie0812 ([#6087](https://github.com/Tencent/tdesign-vue-next/pull/6087))

0 commit comments

Comments
 (0)