@use "../abstract/base" as base;
@use "../abstract/variables" as v;
@use "../abstract/functions" as f;
@use "../abstract/mixins" as m;
@use "../abstract/theme" as t;
@use "../abstract/direction" as d;
@use "../abstract/chips/assist";
@use "../abstract/chips/filter";
@use "../abstract/chips/input";
@use "../abstract/chips/suggestion";

$component: "#{base.$prefix}-chip";
$container: "#{base.$prefix}-chips";

.#{$component} {
--#{base.$prefix}-chip-label-color: #{t.color("on-surface-variant")};
--#{base.$prefix}-chip-leading-color: #{t.color("primary")};
--#{base.$prefix}-chip-trailing-color: #{t.color("on-surface-variant")};
--#{base.$prefix}-chip-checkmark-color: currentColor;
display: inline-flex;
align-items: center;
position: relative;
box-sizing: border-box;
height: v.chip("height");
max-width: 100%;
border: 0;
outline: 1px solid t.color("outline-variant");
outline-offset: -1px;
border-radius: v.chip("border-radius");
background: transparent;
color: var(--#{base.$prefix}-chip-label-color);
user-select: none;
@include m.typography("label-large");
transition: background-color v.motion("duration-short4") v.motion("easing-standard"),
box-shadow v.motion("duration-short4") v.motion("easing-standard");

&--assist { @include assist.tokens; }
&--filter { @include filter.tokens; }
&--input { @include input.tokens; }
&--suggestion { @include suggestion.tokens; }

&__action, &__remove, &__trailing-action {
appearance: none;
position: relative;
display: inline-flex;
align-items: center;
justify-content: center;
flex-shrink: 0;
box-sizing: border-box;
height: 100%;
border: 0;
background: transparent;
color: inherit;
font: inherit;
cursor: pointer;
margin: 0;
border-radius: inherit;
&:disabled { cursor: default; }
&:focus-visible { outline: 3px solid t.color("secondary"); outline-offset: 2px; }
}
&__action {
flex-shrink: 1;
min-width: 0;
padding: 0 16px;
}
&--leading &__action { padding-inline-start: 8px; }
&--trailing &__action { padding-inline-end: 8px; }
&--input &__action { padding-inline: 12px; }
&--input.#{$component}--leading &__action { padding-inline-start: 8px; }
&--input.#{$component}--trailing &__action { padding-inline-end: 8px; }
&--input.#{$component}--avatar &__action { padding-inline-start: 4px; }
&__remove, &__trailing-action {
width: calc(#{v.chip("icon-size")} + 8px);
padding: 0;
padding-inline-end: 8px;
justify-content: flex-start;
@include d.corners($start-start: 0, $end-start: 0, $arm: false);
color: var(--#{base.$prefix}-chip-trailing-color);
}
@include d.arms-at-root(
":where(.#{base.$prefix}-rtl, :host(.#{base.$prefix}-rtl), [dir='rtl'])" +
" :is(.#{$component}__remove, .#{$component}__trailing-action):not(.#{base.$prefix}-ltr *)"
) {
border-radius: inherit;
border-top-right-radius: 0;
border-bottom-right-radius: 0;
}
&:has(> &__remove, > &__trailing-action) { min-width: 88px; }
&:has(> &__remove, > &__trailing-action) &__label { min-width: 42px; }
&__label { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
&__leading-icon, &__trailing-icon, &__checkmark {
display: inline-flex;
width: v.chip("icon-size");
height: v.chip("icon-size");
flex-shrink: 0;
align-items: center;
justify-content: center;
&[hidden] { display: none; }
}
&__leading-icon, &__checkmark { margin-inline-end: 8px; }
&__trailing-icon { margin-inline-start: 8px; }
&--motion &__action {
transition: m.spring(padding-inline-start, fast-spatial), m.spring(padding-inline-end, fast-spatial);
}
&__leading-icon, &__checkmark { overflow: hidden; }
&--motion &__leading-icon, &--motion &__checkmark {
transition: m.spring(width, fast-spatial), m.spring(margin-inline-end, fast-spatial),
m.spring(opacity, slow-effects), display m.spring-duration(fast-spatial) allow-discrete;
@starting-style { width: 0; margin-inline-end: 0; opacity: 0; }
&[hidden] {
width: 0;
margin-inline-end: 0;
opacity: 0;
transition: m.spring(width, fast-spatial), m.spring(margin-inline-end, fast-spatial),
m.spring(opacity, fast-effects), display m.spring-duration(fast-spatial) allow-discrete;
}
}
&__leading-icon { color: var(--#{base.$prefix}-chip-leading-color); }
&__trailing-icon { color: var(--#{base.$prefix}-chip-trailing-color); }
&__checkmark { color: var(--#{base.$prefix}-chip-checkmark-color); }
&__leading-icon svg, &__trailing-icon svg, &__checkmark svg, &__remove svg, &__trailing-action svg { width: 18px; height: 18px; }
&--avatar &__leading-icon { width: 24px; height: 24px; border-radius: 50%; overflow: hidden; }
&--disabled.#{$component}--avatar &__leading-icon { opacity: 0.38; }
&--avatar &__leading-icon :is(svg, img) { width: 24px; height: 24px; object-fit: cover; }

&--selected {
background: t.color("secondary-container");
outline-color: transparent;
--#{base.$prefix}-chip-label-color: #{t.color("on-secondary-container")};
--#{base.$prefix}-chip-leading-color: #{t.color("on-secondary-container")};
--#{base.$prefix}-chip-trailing-color: #{t.color("on-secondary-container")};
}
&--input:not(.#{$component}--disabled) {
&.#{$component}--selected, &:hover, &:has(:focus-visible), &:focus-visible, &:active {
--#{base.$prefix}-chip-leading-color: #{t.color("primary")};
--#{base.$prefix}-chip-checkmark-color: #{t.color("primary")};
}
}
&--elevated {
outline-color: transparent;
background: t.color("surface-container-low");
box-shadow: f.get-elevation(1);
&.#{$component}--selected { background: t.color("secondary-container"); }
&:hover:not(.#{$component}--disabled) { box-shadow: f.get-elevation(2); }
&:active:not(.#{$component}--disabled) { box-shadow: f.get-elevation(1); }
}
&--filter.#{$component}--selected:not(.#{$component}--elevated):hover:not(.#{$component}--disabled) { box-shadow: f.get-elevation(1); }
&--filter.#{$component}--selected:not(.#{$component}--elevated):active:not(.#{$component}--disabled) { box-shadow: none; }
&:is(:has(:focus-visible), :focus-visible):not(.#{$component}--disabled):not(.#{$component}--selected):not(.#{$component}--elevated):not(.#{$component}--pointer-focus) { outline-color: t.color("on-surface-variant"); }
&--assist:is(:has(:focus-visible), :focus-visible):not(.#{$component}--disabled):not(.#{$component}--elevated):not(.#{$component}--pointer-focus) { outline-color: t.color("on-surface"); }
&:focus-visible:not(.#{$component}--pointer-focus)::before {
content: "";
position: absolute;
inset: -5px;
border: 3px solid t.color("secondary");
border-radius: calc(#{v.chip("border-radius")} + 5px);
pointer-events: none;
}
&::after {
content: "";
position: absolute;
inset: 0;
border-radius: inherit;
background: currentColor;
pointer-events: none;
opacity: 0;
}
&:hover:not(.#{$component}--disabled)::after { opacity: v.state("hover-state-layer-opacity"); }
&:is(:has(:focus-visible), :focus-visible):not(.#{$component}--disabled):not(.#{$component}--pointer-focus)::after { opacity: v.state("focus-state-layer-opacity"); }
&:active:not(.#{$component}--disabled):not(:has(> .#{$component}__action:active > .#{base.$prefix}-ripple))::after { opacity: v.state("pressed-state-layer-opacity"); }
&--dragged { box-shadow: f.get-elevation(4); }
&--dragged:not(.#{$component}--disabled)::after { opacity: v.state("dragged-state-layer-opacity"); }
&--disabled {
outline-color: t.alpha("on-surface", 0.12);
box-shadow: none;
--#{base.$prefix}-chip-label-color: #{t.alpha("on-surface", 0.38)};
--#{base.$prefix}-chip-leading-color: #{t.alpha("on-surface", 0.38)};
--#{base.$prefix}-chip-trailing-color: #{t.alpha("on-surface", 0.38)};
&.#{$component}--selected, &.#{$component}--elevated { background: t.alpha("on-surface", 0.12); outline-color: transparent; }
.#{base.$prefix}-ripple { display: none; }
}
&__action::before { content: ""; position: absolute; inset-inline: 0; inset-block: -8px; }
&__remove::before, &__trailing-action::before {
content: "";
position: absolute;
top: 50%;
@include d.end(0);
width: 48px;
height: 48px;
transform: translateY(-50%);
}
}

.#{$container} {
display: flex;
flex-wrap: wrap;
gap: v.spacing("2");
width: 100%;
min-height: fit-content;
height: auto;

&__label {
@include m.typography("label-medium");
font-size: 16px;
color: t.color("on-surface-variant");
display: block;
width: 100%;
}

&--with-label {
display: flex;
flex-direction: column;

&.#{$container}--label-end {
flex-direction: column-reverse;

.#{$container}__label {
margin-bottom: 0;
margin-top: v.spacing("2");
}
}
}

&__container {
display: flex;
flex-wrap: wrap;
gap: v.spacing("2");
width: 100%;
}

&--scrollable {
.#{$container}__container {
flex-wrap: nowrap;
overflow-x: auto;
overflow-y: auto;
-webkit-overflow-scrolling: touch;
scroll-behavior: smooth;
padding: 6px 6px v.spacing("2");
margin: -6px -6px 0;

position: relative;
white-space: nowrap;

&::-webkit-scrollbar {
height: 4px;
}

&::-webkit-scrollbar-thumb {
background-color: t.alpha("on-surface", 0.2);
border-radius: 4px;
}

&::-webkit-scrollbar-track {
background-color: t.alpha("on-surface", 0.05);
border-radius: 4px;
}

scrollbar-width: thin;
scrollbar-color: t.alpha("on-surface", 0.2)
t.alpha("on-surface", 0.05);

.#{$component} {
flex-shrink: 0;
}
}
}

&--vertical {
.#{$container}__container {
flex-direction: column;
align-items: flex-start;
gap: v.spacing("2");
}
}
}
