From fde88c4beccaa4f886afe84cdf71d002421c3a21 Mon Sep 17 00:00:00 2001 From: Justineo Date: Fri, 18 Oct 2024 11:52:02 +0800 Subject: [PATCH 1/6] feat(kbutton): add appearance="none" to button --- .stylelintrc.mjs | 5 +- sandbox/pages/SandboxButton.vue | 130 ++++++++++++++++++++++++++++- src/components/KButton/KButton.vue | 114 +++++++++++++++++-------- src/types/button.ts | 3 +- 4 files changed, 212 insertions(+), 40 deletions(-) diff --git a/.stylelintrc.mjs b/.stylelintrc.mjs index d1376c798d..d7e57e064c 100644 --- a/.stylelintrc.mjs +++ b/.stylelintrc.mjs @@ -12,7 +12,10 @@ export default { rules: { // Disallow relative font units since we don't know the base font size in other apps 'unit-disallowed-list': ['rem', 'em'], - 'order/properties-alphabetical-order': true, + 'order/properties-order': [ + ['all'], + { 'unspecified': 'bottomAlphabetical' }, + ], '@kong/design-tokens/use-proper-token': true, '@stylistic/indentation': [2, { baseIndentLevel: 0 }], // Only allow @kong/design-tokens or `--kong-ui-*` CSS custom properties diff --git a/sandbox/pages/SandboxButton.vue b/sandbox/pages/SandboxButton.vue index 847b0a1c84..a3f8e6cb75 100644 --- a/sandbox/pages/SandboxButton.vue +++ b/sandbox/pages/SandboxButton.vue @@ -13,7 +13,7 @@ title="appearance" >
@@ -51,6 +54,12 @@ > Danger + + None +
Disabled + + Disabled +
Danger + + None + Danger + + None + Danger + + + None +
@@ -292,6 +329,10 @@ Danger + + + None +
@@ -319,6 +360,13 @@ Danger + + + None +
@@ -347,6 +395,13 @@ Danger + + None + +
@@ -365,6 +420,10 @@ Danger + + None + +
@@ -392,6 +451,13 @@ Danger + + None + +
@@ -429,6 +495,13 @@ > + + +
@@ -452,6 +525,12 @@ > + + +
+ + + +
+ + + +
+ + Custom + + + Custom +
@@ -507,4 +616,23 @@ const test = () => { gap: $kui-space-50; } } + +/* Low-specificity styles should be able to override */ +.custom-button { + background-color: #42b883; + border-radius: 8px; + color: #fff; + font-size: 16px; + font-weight: 600; + padding: 8px 16px; + + &:hover:not(:disabled) { + background-color: #33a06f; + transition-duration: .2s; + } + + &:disabled { + opacity: 0.3; + } +} diff --git a/src/components/KButton/KButton.vue b/src/components/KButton/KButton.vue index a8cb98194f..1b65ec459a 100644 --- a/src/components/KButton/KButton.vue +++ b/src/components/KButton/KButton.vue @@ -2,10 +2,12 @@ @@ -86,6 +88,24 @@ const buttonType = computed((): string => { return 'button' }) +const buttonAppearance = computed((): ButtonAppearance | [ButtonAppearance, string] => { + // If the appearance is invalid, output both to keep backwards compatibility + // in case some of the tests rely on the invalid appearance output + if (Object.values(ButtonAppearances).indexOf(props.appearance) === -1) { + return ['primary', props.appearance] + } + + return props.appearance +}) + +const buttonSize = computed((): ButtonSize | null => { + if (props.appearance === 'none') { + return null + } + + return props.size +}) + /** * Strips falsy `disabled` attribute, so it does not fall onto native elements. * Vue 3 no longer removes attribute if the value is boolean false. Instead, it's set as attr="false". @@ -110,6 +130,25 @@ const strippedAttrs = computed((): typeof attrs => { return modifiedAttrs }) +const stop = (event: Event) => { + event.preventDefault() + event.stopPropagation() +} + +const listeners = computed(() => { + if (!props.disabled || buttonType.value === 'button') { + return {} + } + + // Only prevent clicks on disabled links, emulating the same behavior as a disabled button + return { + clickCapture: stop, + dblclickCapture: stop, + mousedownCapture: stop, + mouseupCapture: stop, + } +}) + onMounted(() => { if (slots.icon) { console.warn('KButton: `icon` slot is deprecated. Please slot an icon into the `default` slot instead. See the migration guide for more details: https://kongponents.konghq.com/guide/migrating-to-version-9.html#kbutton') @@ -124,36 +163,9 @@ export default {