@@ -6,6 +6,11 @@ import { usePrefixClass } from '@tdesign/shared-hooks';
66
77export 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 >
0 commit comments